Crontab Scheduled Tasks

Source: Internet
Author: User

With the crontab command, we can execute specified system instructions or shell script scripts at a fixed interval of time. The units of the time interval can be any combination of minutes, hours, days, months, weeks, and more. This command works well for periodic log analysis or data backup.

Command format:crontab [-u user] [-l|-e|-r]

    • -u User: Used to set a user's crontab service
    • -L: Lists the contents of a user's crontab file. If you do not specify a user, the default is that of the current user.
    • -E: Edits a user's crontab file. If you do not specify a user, the default is that of the current user.
    • -r: Deletes a user's crontab file. If you do not specify a user, the default is the current user. To avoid accidental deletion, you can use Crontab-r > Jasoncronbackup to back up.
    • -I: Give a confirmation prompt when deleting a user's crontab file.
File formats for crontab
分 时 日 月 星期 要运行的命令13410 * /bin/echo"echo" //每年10月4号3点1分定时输出"echo"字符串
Create steps
    1. Create the crontab file, named [Filename]cron, for example, my jasoncron. File contents:
# The program that executes the script shell  = /bin/sh  # environment variable path  =< Span class= "Hljs-regexp cye-lm-tag" >/usr/local  /sbin:/usr  /local/bin  :/sbin  :/bin  :/usr/sbin  :/usr/bin   #每分钟执行输出  1 . * * * * * /bin/echo   ' Hello World '  >> /home/jason  / Cronout   

 
2. The Execute command $ crontab jasoncron implicitly considers the current user to add a cron file. by crontab
The-l command can list the current cron content. In this case, a file with the user name is generated in path/var/spool/cron/crontabs, with the contents of the cron file added earlier. For example, the contents of my build/var/spool/cron/crontabs/jason file are:

# do not EDIT this file-edit the master and reinstall.# (Jasoncron1 installed on Fri 6 14:49:12)# (Cron version--$Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)# programs that execute scriptsSHELL=/bin/sh# Environment variablesPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin#每分钟执行输出* * * * */bin/echo ' Hello World '>>/home/jason/cronout
Precautions
    1. The newly created cron job will not execute immediately, at least 2 minutes. If you restart Cron, it will be executed immediately.
    2. When crontab fails, you can try/etc/init.d/crond Restart to solve the problem. Or check the log to see if a job has execution/error tail-f/var/log/cron.
    3. Don't run crontab-r. It removes the user's crontab file from the crontab directory (/var/spool/cron). All crontab of the user have been deleted.
    4. In Crontab, the% has a special meaning, indicating the meaning of the line break. If you want to use the words must be escaped%, such as the frequently used date ' +%y%m%d ' in the crontab is not executed, should be replaced by the date ' +%y%m%d '.
    5. system-level task scheduling mainly completes some maintenance operation of the system, user-level task scheduling mainly completes the user-defined tasks, you can put the user-level task scheduling to the system-level task scheduling to complete (not recommended), but in turn, the root user's task scheduling operation can be ”crontab –uroot –e”to set, or you can write the dispatch task directly to the/etc/crontab file, it is important to note that if you want to define a task that periodically restarts the system, you must put the task into the/etc/crontab file, Even the task of creating a timed restart of the system under the root user is not valid.
Problems

Sometimes we create a crontab, but this task cannot be executed automatically, but it is not a problem to perform this task manually, which is usually caused by not configuring environment variables in the crontab file.

When you define multiple dispatch tasks in a crontab file, you need to make a special note of the settings of the environment variables, because when we perform a task manually, it is in the current shell environment, the program can certainly find the environment variable, and the system automatically executes the task schedule, it will not load any environment variables, therefore, You need to specify all the environment variables that are required for the task to run in the crontab file, so that the system does not have a problem when it executes the Task Scheduler.

Don't assume that Cron knows the special circumstances you need, and it doesn't really know. So you have to make sure that you provide all the necessary path and environment variables in the shelll script, except for some auto-set global variables. So note the following 3 points:

    • Write the global path when the file path is involved in the script;
    • when script execution is used with Java or other environment variables, the environment variables are introduced through the source command , such as:
cat start_cbp.sh!/bin/shsource /etc/profileexport RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf/usr/local/jboss-4.0.5/bin/run.sh -c mev &
    • When you manually execute the script OK, but crontab is not executed, it is likely that the environment variables, you can try to directly introduce environment variables in crontab to solve the problem. such as:
0 * * * * . /etc/profile;/bin/sh /var/www/java/audit_no_count/bin/restart_audit.sh
Reference

Http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html#id2

Crontab Scheduled Tasks

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.