Demand scenarios

Students in the development background often need to write some scripts to start and run regularly, usually using crontab. Sometimes there are even more demanding requirements: scripts not only need to be timed, but they also need to be mutually exclusive (only one process is running at a time, and the last one will not start this time), and even need to set a timeout (the process will be terminated automatically when running for too long) and a timeout alarm. So how do you do that? The answer: Crontab starts tasks on schedule, Flock guarantees mutual exclusion, timeout sets timeout, and alarm scripts

Introduction to crontab, flock, timeout

The use of the crontab

The crontab is a scheduled task implementation program in Linux that can only implement minute-level scheduled tasks. For details, see crontab Scheduled Task

Points to note:

  • If the task starts every 8 minutes, the task will be executed every 8 minutes starting from 0 minutes on the hour. Therefore, the task will be executed at 56 minutes and again at 0 minutes on the next day.
  • The path of the script should use the absolute path or use the CD command to the absolute path

The use of the flock

Flock ensures that only one script singleton is executed. Mostly in mutually exclusive non-blocking mode, waiting times are useful, but I use them less often.

See using the flock command to ensure script singletons

Points to note:

  • Flock mutually exclusive files preferably use absolute paths and end with.lock
  • The corresponding files are generated when using flock and won’t be automatically cleared when the following commands are complete

The use of a timeout

Refer to timeout(1)-Linux man Page

Using the example

#-9 is sent after the timeout, and failed is displayed after the timeout execution
timeout -s 9 5 sleep 20 || echo 'failed'
#Execute the following script without timeout to output SUCCESS
timeout -s 9 10 sleep 5 && echo 'success'
Copy the code

Points to be aware of

  • The return code for the normal end of timeout is 0
  • Timeout The return code for the kill end is 124

Use examples

The following script is executed periodically: task.sh

Echo 'start at: ' 'date'#About 3 minutesSleep 180 echo 'end at: ' 'dateCopy the code

The timeout alarm notification script alarm.sh is as follows:

Append the timeout to alarm.log
echo 'the timeout at:' `date` >>alarm.log
# Other alarm measures, email, SMS....
Copy the code

Since the above scheduled task will end in about 3 minutes, our timeout time will be more than 3 minutes (the specific amount can be set according to different situations). Assume that the crontab file will be executed once in 6 minutes with mutual exclusion and non-blocking, and that the alarm script will be executed in 10 minutes with timeout, then the final crontab file is as follows

*/6 * * * * cd /data/projec && flock -xn ./task.lock -c 'timeout -9 600 sh task.sh || sh alarm.sh'
Copy the code

Points to be aware of

If task.sh starts a subprocess for processing, run the wait command at the end of the task.sh command to wait for all subprocesses to complete. Otherwise, timeout is invalid

cd /data/project
nohub python3 main.py >main.log 2>&1 &
wait
Copy the code