In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to run scripts regularly in Linux? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1. Install crontab
[root@CentOS ~] # yum install vixie-cron [root@CentOS ~] # yum install crontabs
Vixie-cron package is the main program of cron
The crontabs package is a program that installs, uninstalls, or enumerates tables used to drive cron daemons.
two。 Enable the crontab service
Service crond start / / start the service
Start and shut down the cron service in the following ways:
Service crond start / / start the service
Service crond stop / / shut down the service
Service crond restart / / restart the service
Service crond reload / / reload configuration
View crontab service status: service crond status
Start the crontab service manually: service crond start
To see if the crontab service is set to boot, execute the command: ntsysv
Join the boot and start automatically:
Chkconfig-level 35 crond on
In addition, the ntsysv and chkconfig commands are introduced by the way:
On the ntsysv command is a class graphical interface management mode to set boot, need to be installed before you can use, yum install-y ntsysv installed only need to run a ntsysv to appear a similar image management interface.
Arrow keys: you can move between services in the middle box
Space bar: it can be used to select the service you need. [*] means to start.
Tab key: you can move between boxes, OK, and Cancel
[F1] key: you can display a description of the service.
Whether the form setting of the chkconfig command line is self-booting or querying the operation of a service at 6 boot levels.
Set the crond service to start automatically when booting:
[root@CentOS ~] # chkconfig crond on
View the operation of crond services at each boot level
[root@CentOS ~] # chkconfig-list crond
Crond 0: off 1: off 2: enable 3: enable 4: enable 5: enable 6: close
You can see that the crond service is started automatically at levels 2, 3, 4 and 5.
Cancel booting and automatically start the crond service:
[root@CentOS ~] # chkconfig crond off
3. Set up the script to be executed
There are two ways to schedule new tasks:
1) enter: crontab-e on the command line and then add the corresponding task, and wq saves and exits.
2) directly edit the / etc/crontab file, namely vi / etc/crontab, and add the corresponding tasks.
Crontab-e configuration is for a user, while editing / etc/crontab is a task for the system
View scheduled tasks
Crontab-l / / lists all current scheduled tasks
Crontab-l-u jp / / lists all scheduled tasks for user jp
Delete task scheduling work
Crontab-r / / Delete all task scheduling work
Edit vim / etc/crontab directly. The default file format is as follows:
SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/# For details see man 4 crontabs# Example of job definition:#.-minute (0-59) # |.-hour (0-23) # | |.-day of month (1-31) # |.-month (1-12) OR jan,feb,mar Apr. # |.-day of week (0-6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat# | # * user-name command to be executed
The explanation of this text is already quite intuitive, in which
Asterisk (*): represents all possible values. For example, if the month field is an asterisk, it means that the command operation is performed every month after the constraints of other fields are met.
Comma (,): you can specify a list range with values separated by commas, for example, "1, 2, 5, 7, 8, 9"
Middle bar (-): you can use the middle bar between integers to represent a range of integers, for example, "2-6" means "2pens 3pr 4je 5pl 6"
Forward slash (/): the interval frequency of the time can be specified with a forward slash, for example, "0-23gam2" indicates that it is executed every two hours. At the same time, a forward slash can be used with an asterisk, such as * / 10, which, if used in the minute field, means that it is executed every ten minutes.
Here are a few examples that basically cover some common situations:
Example 1
5 * ls designated 5 minutes per hour to execute ls command 30 5 * ls designated 5:30 every day to execute ls command 307 8 * * ls designated 7:30 on the 8th of each month to execute ls command 305 8 6 * ls designated 5 June of each year : 30 execute ls command 30 5 8 6 * ls specify 8 June every year 5:30 execute ls command 30 6 * 0 ls specify 6:30 every Sunday execute ls order 30 3 10 ls 20 * * ls execute ls command 25 8-11 * * ls every day at 3:30 on the 10th and 20th of each month Execute the ls command at the 25th minute from 8 to 11:00 * / 15 * ls execute the ls command 30 6 * / 10 * * ls every 15 minutes The ls command 22 4 * root run-parts / etc/cron.daily# is executed at 6:30 every 10 days, and all executables in the / etc/cron.daily directory are executed as root at 4:22 every day, and the run-parts parameter indicates that all executables in subsequent directories are executed.
Example 2
# restart apache30 21 * / usr/local/etc/rc.d/lighttpd restart# at 21:30 every night * / restart apache45 41 at 4: 45 on the 1st, 10th and 22nd of each month * / usr/local/etc/rc.d/lighttpd restart# every Saturday, 1: 10 restart on Sunday apache10 1 * * 6 apache10 / usr/local/etc/rc.d/lighttpd restart# restart apache0,30 18-23 * / usr/local/etc/rc.d/lighttpd restart# every 30 minutes between 18: 00 and 23: 00 every Saturday 11: 00 pm restart apache0 23 * * 6 / usr/local/etc/rc.d/lighttpd restart# between 11:00 and 7: 00 a.m. Restart apache0 23-7 mon-wed 1 * / usr/local/etc/rc.d/lighttpd restart# every hour restart apache0 * / 1 * / usr/local/etc/rc.d/lighttpd restart# on the 4th of each month and restart apache0 11 4 * mon-wed / usr/local/etc/rc.d/lighttpd restart# every Monday to Wednesday at 4: 00 on January 1st restart apache0 4 1 jan * / usr/local / etc/rc.d/lighttpd restart# synchronizes time 0swap 30 * / usr/sbin/ntpdate 210.72.145.44 every half hour
Be careful
The * * 1 * command is executed every minute of every hour.
The minute of each hour must be specified, that is, the first * sign must be changed to a numeric value.
Because the * sign means every minute.
In addition, there is no difference between / 1 and / 1, both once an hour.
If you set * / 2, it is actually the number of hours divisible by 2 rather than 2 hours after the start of the timing setting, such as 9 o'clock to 10:00.
You may end up with the following problem
Input crontab-l display under root user
No crontab for root for example:
[root@CentOS ~] # crontab-l
No crontab for root
This question is very simple. Also type crontab-e under the root user.
Press Esc, press: wq enter
There will be no problem typing crontab-l
What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.
After reading the above, have you mastered how to run scripts regularly in Linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.