Linux auto-execute crontab study notes

  
On the Linux platform, if you need to implement the task scheduling function, you can write a cron script to achieve. Executing a task at a certain frequency Linux will start the crond process by default. The crond process does not require the user to start or shut down. The crond process is responsible for reading the scheduled task and executing it. The user only needs to write the corresponding scheduling script into the cron scheduling configuration file. The cron schedule file has the following: crontab
cron.d
cron.daily
cron.hourly
cron.monthly
cron.weekly

Instead of performing hourly monthly weekly, you can write the corresponding crontab to the crontab or cron.d directory. Example: Execute the script /opt/bin/test-cron.sh every minute. You can create a new script echo-date.sh in cron.d with the contents */1 * * * * root /opt/bin/test-cron. Sh can also run the task at the specified time. You can also use the at command to control the running of the task at the specified time. For example: at -f test-cron.sh -v 10:25 where -f specifies the script file, -v specifies the runtime quote:ea946d690b ="lophyxp"] First use contab -l &gt;contabs.tmp to export the configuration of contab. Then edit the contabs.tmp file. Add a line in the following format: minute hour day month week command such as 10 3 * * 0,6 hello is to execute the hello program every Saturday and Sunday at 3:10. 15 4 * * 4-6 hello is to execute the hello program from 4 to 15 o'clock from Thursday to Saturday. Then use the contab contabs.tmp command to import the new configuration. It is generally not recommended to directly modify the relevant configuration files under /etc/. The method to start the cron process: /etc/init.d/crond start Start the cron process setup command: chkconfig --add crond method 2: Add cron to the startup script: # rc-update add vixie-cron defaultcrontab - l #View your task crontab-e#Edit your task crontab-r# Delete the user's crontab content example 2: System cron settings: /etc/crontab Through the /etc/crontab file, you can set the system to execute regularly Task, of course, to edit this file, you must have root privileges 0 7 * * * root mpg123 ~/wakeup.mp3 Time-of-day month and week example: 0 4 * * 0 root emerge --sync && emerge -uD World #4 am every Sunday, update system 0 2 1 * * root rm -f /tmp/* #2:00 am on the 1st of the month, clean up the files under /tmp 0 8 6 5 * root mail robin < /Home/galeki/happy.txt # Send a letter to Robin every May 6th, wishing him a happy birthday. If I want to execute a command every 2 minutes, or I want to be at 6 or 12 every day. , 18 points to execute commands, and the like, can be set by “ /” and “ , ” to set: */2 * * * * root ............... # Executed every two minutes........ 0 6,12,18 * * * root ............... #6, 12, 18 every day. ....... every two hours 0 */2 * * * echo "have a break now." >&gt; /tmp/test.txt between 11pm and 8am Two hours, 8:00 am, 23:00-7/2, 8 * * * echo "have a good dream:)" &gt;&gt; /tmp/test.txt Every week from Monday to Wednesday 11:00 11 * 4 1-3 command line January 1st at 4 am 0 4 1 1 * command line harvest: you can put some things you often do, simplify workload, such as running a check server status weekly, view reports, kill a number of processes and so on & hellip; & hellip;

about the Crontab, Wikipedia entries Crontab very good. Unfortunately, this address is not accessible to agents in China.
Although the introduction of Crontab is everywhere, read the term in detail, and the harvest is still there. The name Crontab comes from "chronos", an ancient Greek, “time”.

Common traps
Every SA, DBA or ordinary Unix user, used for the first time Crontab has problems. Common errors for running Crontab include the following: 1) A new Cron JOB has been created for testing purposes, and the interval must be more than two minutes, otherwise the JOB will not be dispatched. If you must ignore the two-minute load configuration time difference, you can do so by restarting the Cron Daemon.
2) Things to note when starting the X Window program from Crontab: So either initialize "DISPLAY=:0.0" before the program, or append the parameter after the application --display :0.0
3) The % in must be escaped: \\%. My personal opinion is not to take this parameter in the command line, just write it into the script, and then dispatch the script.
In fact, I think the most common problem with using Crontab is often because the environment variables are wrong. I often see people in the forum asking: Why is my Crontab created and not executing? When I am ready to create a Cron JOB, many people like to run it from the command line, because the environment variable is automatically brought in with the shell. In Crontab, JOB cannot be executed because the correct environment variable is not found. This small problem is like a smallpox, and I remember it after a lesson.

A trick that must be used
After each JOB is executed, the system will automatically send the output to the current system user. Over time, it is very much, and it will even blast the entire system. So it is necessary to perform a redirection process after each JOB command: >/dev/null 2>&1 . The premise is that the commands in the job need to be processed normally, such as appending to a specific log file. Attachment: The format of Crontab is as follows:
* Comma (',') specifies the list value. For example: "1,3,4,7,8"* The horizontal line ('-') specifies the range value as "1-6", for "1,2,3,4,5,6"* The asterisk ('*') stands for all possible values
Linux (open source system seems to be fine) and there is a "/" available. On the Minute field, */15 means every 15 minutes. This feature is not available on commercial Unix, such as AIX.
# Use the hash sign to prefix a comment # +---------------- minute (0 - 59) # |   +------------- hour (0 - 23) # |   |   +---------- day of month (1 - 31) # |   |   |   +------- month (1 - 12) # |   |   |   |   +---- day of week (0 - 7) (Sunday=0 or 7) # |   |   |   |   |  # * * * * * command to be executed

Copyright © Windows knowledge All Rights Reserved