Linux Auto-Execute SH script

Source: Internet
Author: User
Tags set time

Crontab Linux User Time Schedule table

Crontab-l Viewing the time schedule table

CRONTAB-E editing the time schedule table

Crontab-r Delete a time schedule table

The General Crontab service is automatically enabled.

Service Crond Status command to see if crontab is enabled

Example: Executing/opt/start.sh at a specified time, executing/opt/stop.sh at a specified time

Crontab-e into VI editing interface

*/2 * * * * sh/opt/start.sh #每隔2分钟执行一次start. Sh script

*/4 * * * * sh/opt/stop.sh #每隔4分钟执行一次stop. Sh script

---------------------------------------

The format of the schedule table is as follows:
F1 F2 F3 f4 f5 program

Where F1 is expressed in minutes, F2 represents hours, F3 represents the day of the month, F4 represents the month, and F5 represents the day of the one week. Program represents the programs to execute.
When F1 is * indicates that every minute the program,f2 is executed, the program is executed every hour, and the rest of the analogy
When the F1 is a-B, it is executed from the time of the minute to the minute of the hour, and the F2 is a-B indicating that it is executed from the first and the other, and the other analogy
When F1 is */n, it is executed once every n minutes, F2 is */n for every n-hour interval, and the rest of the analogy
When F1 is a, B, C,... A, B, C,... Minutes to execute, F2 for a, B, C,... The time indicated that the first, B, c ... Hours to execute, and the rest of the analogy

The user can also store all the settings in the file file, using the crontab file to set the time schedule.
Example:

The/bin/ls is performed at the first 0 minutes of every day per month:
0 7 * * */BIN/LS

In December, the/usr/bin/backup is performed every 20 minutes in the morning from 6 to 12 hours per day:
0 6-12/3 */usr/bin/backup

Monday to Friday every 5:00 send a letter to [email protected]:
0 * * 1-5 mail-s "HI" [email protected] </tmp/maildata

Every day of the month 0:20 midnight, 2:20, 4:20 .... Perform echo "haha"
0-23/2 * * * echo "haha"

Attention:
When the program executes at the time you specify, the system will send you a letter showing what the program is doing, and if you do not wish to receive such a letter, please add >/dev/null 2>&1 after each line.

Name: Date
Usage rights: All users
How to use:

Date [-u] [-D DATESTR] [-S datestr] [--UTC] [--universal] [--DATE=DATESTR] [--SET=DATESTR] [--help] [--version] [+format] [MMDDHHMM[[CC]YY][.SS]]

Description
Date can be used to display or set the system's Day and time, in terms of display, the user can set the format to be displayed, the format is set to a plus sign followed by a number of tags, where the list of available tags is as follows:

In terms of time:
%: Printed%
%n: Next line
%t: Jump Grid
%H: Hours (00..23)
%I: Hours (01..12)
%k: Hours (0..23)
%l: Hours (1..12)
%M: minutes (00..59)
%p: Show local AM or PM
%r: Direct Display time (12-hour format, HH:MM:SS [ap]m)
%s: Number of seconds from January 1, 1970 00:00:00 UTC to date
%s: Seconds (00..61)
%T: Direct Display time (24-hour system)
%x: Equivalent to%h:%m:%s
%Z: Show Time zone

Date aspects:
%a: Day of the Week (Sun. Sat)
%A: Day of the Week (Sunday). Saturday)
%b: Month (Jan). DEC)
%B: Month (January). December)
%c: Display date and time directly
%d: Day (01..31)
%d: Show date directly (MM/DD/YY)
%h: With%b
%j: The first day of the Year (001..366)
%m: Month (01..12)
%u: Week of the Year (00..53) (in Sunday as the first day of the week)
%w: The day of the Week (0..6)
%W: Week of the Year (00..53) (with Monday as the first day of the week)
%x: Show date directly (MM/DD/YY)
%y: Last two digits of the year (00.99)
%Y: Full year (0000..9999)

If you do not start with a plus, it means to set the time, and the time format is MMDDHHMM[[CC]YY][.SS], where mm is the month, DD is the day, HH is the hour, MM is minutes, CC is the first two digits of the year, YY is the year after two digits, SS is the number of seconds
Parameters:
-D datestr: Displays the time set in the DATESTR (non-system time)
--HELP: Display Auxiliary message
-S DATESTR: Set the system time to the time set in Datestr
-U: Show current GMT
--version: Show version number

Example:
Display the time after skip, and then display the current date:
Date +%t%n%d

Show the month and day number:
Date +%b%d

Display date and set time (12:34:56):
Date--date 12:34:56

Attention:

When you don't want to have a meaningless 0 o'clock (say 1999/03/07), you can insert a symbol into the tag, for example, date +%-h:%-m:%-s will remove the meaningless 0 in seconds and minutes, as if the original 08:09:04 would change to 8:9:4. In addition, the system time can be set only if the authority (for example, Root) is acquired.

When you change the system time as root, remember to use CLOCK-W to write the system time to the CMOS so that the system time will continue to hold the latest correct value the next time you reboot.

Linux Auto-Execute SH script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.