How to implement second-level scheduled tasks with Linux Crontab Shell script

How to implement second-level scheduled tasks with Linux Crontab Shell script

1. Write Shell script crontab.sh

#!/bin/bash 
step=1 #The number of seconds between steps cannot 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. Enter the following statement in crontab -e, then press :wq to save and exit

# mh dom monitor command 
* * * * * /home/www/php/crontab/crontab.sh

Linux second-level scheduled task implementation

Ideas:

Linux itself does not support second-level timing. To achieve this, you can run a script at a fixed time (for example, once every minute). The content of this script is actually an infinite loop execution code, that is, it keeps executing.

Note: If the time taken for a scheduled program to execute is too long, for example, it takes more than 1 second to execute, you need to seelp, that is, let the script sleep for a while in the infinite loop.

The following demonstrates a shell script's second-level timing task:

1.crontab -e executes the inviteFriend.sh script every minute

#2 seconds to run - Invite friends 3.0

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

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

Write the following content:

#!/bin/bash
step=2 #The number of seconds between steps cannot 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

wqSave and exit

Note: After saving, you need to authorize the script.

chmod 777 filename

Summarize

The above is the method that I introduced to you to use Linux Crontab Shell script to implement second-level scheduled tasks. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time!

You may also be interested in:
  • Implementing web scheduled tasks on AspNet Core
  • How to use crontab to execute a scheduled task once a second in Linux
  • Reasons why crontab scheduled tasks are not executed in Linux
  • Using Crontab to implement PHP script timing tasks under CentOS
  • Detailed explanation of Linux scheduled tasks Crontab (recommended)
  • Detailed explanation of using python crontab to set up Linux scheduled tasks
  • Detailed explanation of implementing crontab-like scheduled tasks in dotnet core

<<:  Mysql keeps the existing content and adds content later

>>:  js to write the carousel effect

Recommend

Use of nginx custom variables and built-in predefined variables

Overview Nginx can use variables to simplify conf...

Why are the pictures on mobile web apps not clear and very blurry?

Why? The simplest way to put it is that pixels are...

About the usage and precautions of promise in javascript (recommended)

1. Promise description Promise is a standard buil...

How to check whether a port is occupied in LINUX

I have never been able to figure out whether the ...

vue+el-upload realizes dynamic upload of multiple files

vue+el-upload multiple files dynamic upload, for ...

Common browser compatibility issues (summary)

Browser compatibility is nothing more than style ...

Install Windows Server 2019 on VMware Workstation (Graphic Tutorial)

If prompted to enter a key, select [I don’t have ...

Solution to the error reported by Mysql systemctl start mysqld

Error message: Job for mysqld.service failed beca...

How to install PHP7 Redis extension on CentOS7

Introduction In the previous article, we installe...

MySQL transaction analysis

Transaction A transaction is a basic unit of busi...

CSS and JS to achieve romantic meteor shower animation

1. Rendering 2. Source code HTML < body > &...