The Linux Crontab Shell script implements a second level timed task

  • 2020-12-19 21:21:49
  • OfStack

1. Write Shell script crontab.sh


#!/bin/bash 
step=1 # The number of seconds between can not be greater than 60 
for (( i = 0; i < 60; i=(i+step) )); do 
 $(php '/home/www/php/crontab/crontab.php') 
 sleep $step 
done 
exit 0

2. crontab-e enter the following statement, then :wq save and exit


# m h dom mon dow command 
* * * * * /home/www/php/crontab/crontab.sh

linux second timing task realization

Ideas:

linux itself does not support second-level timing. To implement it, you can run a script regularly (e.g., once per minute). The content of this script is actually 1 endless loop execution code, i.e., 1 straight execution

Note: If the timed program consumes too much time each time, such as 1 second to execute without playing, seelp1 is to let the script sleep 1 during the endless loop

Here is a demonstration of a second timer task for an shell script:

1. crontab-e execute inviteFriend.ES30en script per minute

#2 seconds to run - invite friends 3.0


 * * * * * /webservice/crontab/inviteFriend.sh

2.cd /webservice/crontab/ Switch to vim inviteFriend.sh under directory

Write the following:


#!/bin/bash
step=2 # The number of seconds between can not be greater than 60 

for (( i = 0; i < 60; i=(i+step) )); do
 #$(php '/home/php/crontab/tolog.php') 
 curl -I http://******.com/InviteFriends/sendInviteFriendCoupon
 sleep $step
done
exit 0

wq saves exit

Note: Authorization is given to timed scripts after saving

chmod 777 filename

conclusion

Above is the site to you to introduce Linux Crontab Shell script to achieve the second timing task method, I hope to help you, if you have any questions welcome to leave a message, this site will reply you in time!


Related articles: