Linux planning task crontab learning notes (5): common errors and use cases

Time:2022-1-25

① Environmental variables

Copy code

The code is as follows:

cd ~
pwd

It will go to the root directory of the user you log in to

Copy code

The code is as follows:

ls -a

It can be viewed bash_ Profile this file

Copy code

The code is as follows:

vim .bash_profile

Some environment variables are set here.

You can set a new environment variable and add the following at the end of the file:

Copy code

The code is as follows:

APPDIR=/etc
export APPDIR

:wq! Save exit

Copy code

The code is as follows:

source .bash_profile
echo $APPDIR

If a global variable is used in a scheduled task, for example

Copy code

The code is as follows:

write in

Copy code

The code is as follows:

*/1 * * * * echo $APPDIR >> /tmp/appdir.log

However, it is used at this time

Copy code

The code is as follows:

tail -f /tmp/appdir.log

When viewing the log, you should find that the displayed line is empty. Normally, / etc should be output

$appdir is not recognized at all.

The reason is in bash_ The environment variable set by profile is not recognized by crontab, so pay attention to it.

② When% is used in double quotation marks on the command line, no backslash is added\

③ The or operation is performed between the third and fifth domains

The third field is the day of a month, and the fifth field is the day of the week in a month. For example, run a.sh at 1:59 a.m. on the first Sunday in April

Copy code

The code is as follows:

59 1 1-7 4 * test `date +\%w` -eq 0 && /root/a.sh

%W EQ 0 means a day of the week in which the current day is located. If it is equal to 0, it will be executed

Test indicates whether the judgment is successful

&&Indicates that the test is executed when the previous test is successful

[supplement]

Copy code

The code is as follows:

echo $?

Indicates whether the last command was executed successfully or not, 0 indicates success, and 1 indicates failure

④ Misuse of minute setting

[run every two hours]

Correct writing

Copy code

The code is as follows:

0 */2 * * * date

[another example]

Copy code

The code is as follows:

1-20/2 * * * * COMMAND

Indicates the 1st, 3rd, 5th, 7th, 9th 19 minute run command

============

[comprehensive case]

[example 1]Crontab can only be set to execute a command every minute at least. If you want to execute a command every half a minute, how can you do it?

——This function can be completed through the sleep command of shell script and crontab

Copy code

The code is as follows:

date && sleep 0.5s && date

For example:

This example indicates that the date is executed first, and then the date is executed after 0.5s

[example 2]

Copy code

The code is as follows:

crontab -e</p>
<p>*/1 * * * * date >> /tmp/date.log
*/1 * * * * seep 30s;date >> /tmp/date.log

The first command ensures that the command is executed every minute

When executing the second command, wait for 30s before executing it

Use the following command to view the write time

Copy code

The code is as follows:

tail -f /tmp/date.log

Recommended Today

Modify user information changeinfo

When judging the persistence layer: Problem: there is such a problem when modifying user information. For example: the user’s email is not required. It was not empty originally. At this time, the user deletes the mailbox information and submits it. At this time, if it is not empty to judge whether it needs to be […]