Linux timed Task Crontab

Source: Internet
Author: User
Tags root access

When using Linux, it is often necessary to perform timed tasks and write some timed scripts using crontab. Recorded here, convenient for later memories.

To facilitate later use, the following online excerpt:

Crond and Crontab

Cron is a server provided by Linux that executes shell commands on a regular basis. Mainly consists of two parts

    • Daemon for Crond:cron services, for periodic scheduling
    • Crontab:cron provides UI for editing scheduling schedules

Crontab the use of the method, you can man a bit, such as:

There are three main options:

    • "-E" edit: With the Vim class Yes, but when you save the exit, he will check the task syntax for you;
    • "-L" lists all tasks;
    • "-R" deletes all tasks;

Method 1:

Use the command crontab-e and edit the timed script directly.

After this execution, the user-defined, will be written to the/var/spool/cron directory, generate a file with the user name consistent with the file content is our editor's timed script.

Method 2:

Edit the timed script using the command vi/etc/crontab.

System-level configuration we will directly configure the/etc/crontab

(User-level) is generally recommended to use CRONTAB-E, so the system will also help to check our configured script syntax.

Basic syntax

The syntax for cron is simple, as follows:

"Dispatch frequency" shell command

Look at an example:

As you can see, the front is minutes, hours, days, months and weeks, and finally the shell command. The minimum frequency for cron scheduling is 1 minutes. The above schedule means: "Every 10:30, execute/some/path/do_something.sh".

The specific meanings of each piece in the above command are as follows:

Significance

Minutes

Hours

Days

Month

Week

Command

Range

0-59

0-23

1-31

1-12

0-7

Shell command

P.S.: 0 and 7 in the "Week" column represent Sunday

P.S.: The last day of each month cannot be directly supported by crontab and needs to be judged by a script, see here.

p.s.: command Best is absolute path

There are also some auxiliary symbols that can be used to write more flexible schedules.

Special characters

Representative meaning

* (asterisk)

Delegates accept at any time, such as the example above, Day,month and week as "*", indicating that the day, regardless of the month of the week, the command is executed at 10:30.

, (comma)

Tie time. As an example:

* 3,6,9,12 * * command

The above means that the command is executed at 3, 6, 9 or 12.

-(minus)

Continuous interval. As an example:

* 9-17 * * command

The above means from 9 to 17, every minute to perform this task.

/n (Slash)

n represents an array, representing every n units, such as every 5 minutes, can be written as follows

*/5 * * * command

Some examples

    • * Mail Benben
    • 0 * * 5 mail All_members < weekily_report_notify//Every Friday 5 o'clock in the afternoon to remind all team members to send weekly

Crontab at the system level

System-level crontab only have root permission to edit, the crontab is a file, the location is/etc/crontab,

The syntax for system-level crontab is slightly different from the above, requiring that command performers be added between frequencies and commands, as shown below, and that you can add some global variables to use in scheduling:

Detailed system-level scheduling can be found here.

Here you need to emphasize several cron-related directories and files in the/etx directory, as shown below

    • CRON.D: directory, in which all files conforming to the schedule syntax in this directory and in subdirectories are executed
    • Cron.deny and Cron.allow: This file records the rejected and allowed to execute the account number, Cron.allow priority is greater than Cron.deny, it is generally recommended to leave only one can
    • Cron.daily/hourly/monthly/weekly: All of these directories are scripts, which are executed at the specified time, respectively.

Absolute path

Although Crontab is executed, it can be understood as a timed trigger for manual execution. However, the path in the execution process is still different, in many cases the manual execution succeeds, but the crontab is unsuccessful, mostly due to the relative path. Therefore, when using crontab, try to use absolute path. As a result, some practices summarize one lesson: If a command can be executed manually, but the crontab fails, then you need to focus on the relevant path problem.

Resources

    • Brother Bird's Linux private cuisine---crontab
    • Last-day-of-month Crontab

Like running Tomcat on a regular basis

Write tomcat_shutdown.sh:

#!/bin/sh

Export java_home=/home/oracle/jdk1.7.0_67/

Export classpath= $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar

Export path= $JAVA _home/bin: $PATH

echo "========begin Stop the tomcat========"

'/home/oracle/apache-tomcat-7.0.29/bin/shutdown.sh '

Write tomcat_startup.sh:

#!/bin/sh

Export java_home=/home/oracle/jdk1.7.0_67/

Export classpath= $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jar

Export path= $JAVA _home/bin: $PATH

echo "========begin start the tomcat========"

'/home/oracle/apache-tomcat-7.0.29/bin/startup.sh '

In the Linux system terminal, enter the command "CRONTAB-E":

In the VI operator interface, enter:

* * * * */home/oracle/auto_exec/tomcat_shutdown.sh

* * * * */home/oracle/auto_exec/tomcat_startup.sh

Use the Crontab-l command to view the settings of a scheduled task.

Use the "tail/var/spool/mail/xxxx" command to view the log of timed task execution

  1. If you are prompted in the log:

    /bin/sh^m:bad interpreter:no such file or directory

    Analysis: This is caused by different system encoding formats: the. sh file that is edited in the Windows system may have invisible characters, so the above exception information is reported in the Linux system execution.

    Solution:(UltraEdit): File-->conversions-->dos->unix can be.

    This problem but let me depressed for a long time Oh ~ ~ ~

  2. 7

    Also, be aware of the permissions of the script files that are scheduled to execute.

    Modified to 777 most insured!!

    Set up auto-start tomcat
    1. Let me show you how to set up Tomcat self-booting under Linux systems. As we all know, under the Linux system, to set up a service from the start, you need to mount under/ETC/RCX.D, but also to write the startup script under/etc/init.d/.

      First supplement: We create a new file under/etc/init.d/tomcat (requires root access)

      Vi/etc/init.d/tomcat

      Write the following code:

      # Tomcat Self-starter script

      #!/bin/sh

      # chkconfig:345 99 10

      # Description:auto-starts Tomcat

      #/ETC/INIT.D/TOMCATD

      # Tomcat Auto-start

      # Source function library.

      #. /etc/init.d/functions

      # source Networking configuration.

      #. /etc/sysconfig/network

      Retval=0

      Export JDK_HOME=/USR/JAVA/JDK1.7.0_45 (please fill in the actual JDK directory)

      Export catalina_home=/home/ldatum/usr/apache-tomcat-7.0.10 (please fill in the real Tomcat directory)

      Export catalina_base=/home/ldatum/usr/apache-tomcat-7.0.10 (please fill in the real Tomcat directory)

      Start ()

      {

      If [-f $CATALINA _home/bin/startup.sh];

      Then

      echo $ "Starting Tomcat"

      $CATALINA _home/bin/startup.sh

      Retval=$?

      echo "OK"

      Return $RETVAL

      Fi

      }

      Stop ()

      {

      If [-f $CATALINA _home/bin/shutdown.sh];

      Then

      echo $ "Stopping Tomcat"

      $CATALINA _home/bin/shutdown.sh

      Retval=$?

      Sleep 1

      PS-FWWU Tomcat | grep apache-tomcat|grep-v grep | Grep-v PID | awk ' {print $} ' |xargs kill-9

      echo "OK"

      # [$RETVAL-eq 0] && rm-f/var/lock/...

      Return $RETVAL

      Fi

      }

      Case "$" in

      Start

      Start

      ;;

      Stop

      Stop

      ;;

      Restart

      echo $ "restaring Tomcat"

      $ stop

      Sleep 1

      $ start

      ;;

      *)

      echo $ "Usage: $ {Start|stop|restart}"

      Exit 1

      ;;

      Esac

      Exit $RETVAL

    2. Once added, add executable permissions to it:chmod +x/etc/init.d/tomcat.

    3. The link to the shell file is then connected to the/etc/rc2.d/directory. The numbers in the/etc/rcx.d/directory for Linux represent the different run level, which is the order in which the boot starts, Ubuntu9.10 There are 0-5 six level, can not be casually connected to other directories, it may be in that directory when the program started Tomcat needs some of the library has not been loaded, with the LN command to link tomcat links Past:ln-s/etc/init.d/ Tomcat /etc/rc2.d/s16tomcat. The naming rules under the RCX.D directory are very specific, more different needs may be the beginning of s, may also be the beginning of K, after the numbers represent their starting order, detailed look at the Readme file in their respective directories.

    4. 6

      The next step is to set the script to run automatically when the system starts, and stop automatically when the system shuts down, using the following command:chkconfig--add Tomcat. If chkconfig is not installed, it is installed with programs such as Apt-get or yum , and the general server version of Linux is already in place.

    5. 7

      Finally, the reboot system is restarted. After rebooting, you will find that your Tomcat has run successfully.

Linux timed Task Crontab

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.