Linux crontab scheduled tasks

Linux crontab scheduled tasks

 

crontab command options:

     -u specifies a user

     -l list a user's task schedule

     -r delete a user's tasks

     -e edit a user's tasks

 

Added:

crontab -e Then add the corresponding task, wq save and exit.

Check:

crontab -l //List all currently scheduled tasks

 

 

A problem with crontab is that it does not always read environment variable parameters from the user profile by default, which often results in successful manual execution of a script, but when trying to make it run periodically in crontab error.

Sometimes it occurs that the timed task cannot identify the environment variables when executing the script, such as: JAVA, SVN, MVN, etc.

This requires adding the following at the beginning of the script:

#!/bin/sh

. /etc/profile

. ~/.bash_profile

 

~ Indicates the home directory of the current user, some environments may have been modified, and .bash_profile cannot be found, deal with it according to the actual situation, if not, comment it out

 

Reference article:

http://www.blogjava.net/freeman1984/archive/2010/09/23/332715.html

 

Note: The environment variables in the execution time may be different from the current environment!

There is no problem with manually executing the script, but when using crontab to execute, there will be problems caused by environment variables

The main solutions are:

1. In the script written by yourself, add at the beginning

#!/bin/sh

. /etc/profile

#. ~/.bash_profile

 

2. Write the relevant environment variables used to the /etc/profile file

That is to say , before crontab is executed, configure the relevant environment variables before executing.

 

 

or:

 

Copy the file (take the todos command as an example)

Copy the todos file (/mydir/re/tofrodos/src) in the installation directory to the /bin directory and give relevant permissions

 

cp /mydir/re/tofrodos/src/todos /bin/

 

chmod 777 /bin/all

 

 

有的定时任务在/etc/crontab中:  cat /etc/crontab

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327101428&siteId=291194637