How to set up scheduled tasks in Linux and Windows

How to set up scheduled tasks in Linux and Windows

Linux

You can use crontab to create scheduled tasks in Linux. The system comes with crontab by default. This demonstration is given in Ubuntu 16.04.

1. Basic use of crontab

#/etc/init.d/cron status # Check the status#/etc/init.d/cron start # Start crontab service#/etc/init.d/cron stop # Stop crontab service#/etc/init.d/cron reload # Reload scheduled tasks#crontab -l # View the scheduled task list

2. Enable logging

The configuration file needs to be modified.

#sudo vim /etc/rsyslog.d/50-default.conf
...
cron.* /var/log/cron.log #Remove the comment character in front of cron...

Restart rsyslog:

#sudo service rsyslog restart

3. Set up scheduled tasks

This demonstration periodically executes a Python script written by myself. Bash scripts or others should be similar. It should be noted that it is best to specify the absolute path of the script. If you find that the problem still cannot be solved, you can switch to the script path for execution first. But, it should be OK.

 ... 0 0 * * * python /home/kdv/Desktop/sync-opensource/sync.py # Scheduled execution of scripts every day or
 @daily cd /home/kdv/Desktop/sync-opensource;python /home/kdv/Desktop/sync-opensource/sync.py

 0 0 1 * mon python /home/kdv/Desktop/sync-opensource/sync.py # Scheduled execution of scripts every week or
 @weekly cd /home/kdv/Desktop/sync-opensource;python /home/kdv/Desktop/sync-opensource/sync.py

Set daily or weekly execution as needed. For more information, please refer to the link.

After setting up the tasks, we can check the task list and reload the tasks as needed.

#crontab -l # You can view the tasks we added #/etc/init.d/cron reload # Reload the scheduled task #vim /var/log/cron.log # View the logs generated by the scheduled task

4. Testing

The figure shows an example of executing a script every 5 minutes for testing.

Left: When the script is running, a log file named after the current time will be generated to record the output results of the script during execution.

Right: The crontab log file. You can see that the script is executed every 5 minutes.

Windows

The Windows system does not have a crontab command, but the Windows system has a command that is similar to the crontab command: the schtasks command. Operate on Win10.

1. Help Documentation

Use the following command to view the help documentation of schtasks to learn more about the command.

C:\Users\Administrator>schtasks /?
SCHTASKS /parameter [arguments]
describe:
 Allows administrators to create, delete, query, change, run, and abort scheduled tasks on local or remote systems.
Parameter List:
 /Create Creates a new scheduled task.
 /Delete Deletes a scheduled task.
 /Query Displays all scheduled tasks.
 /Change Changes the properties of a scheduled task.
 /Run Runs the scheduled task on demand.
 /End Aborts the currently running scheduled task.
 /ShowSid Displays the security identifier that corresponds to the scheduled task name.
 /? Displays this help message.
Examples:
 SCHTASKS
 SCHTASKS /?
 SCHTASKS /Run /?
 SCHTASKS /End /?
 SCHTASKS /Create /?
 SCHTASKS /Delete /?
 SCHTASKS /Query /?
 SCHTASKS /Change /?
 SCHTASKS /ShowSid /?

We can create, query, change and delete tasks, etc. If you are not familiar with the corresponding sub-commands, such as the create command, you can use SCHTASKS /Create /? to further view the detailed instructions.

2. View the system default tasks

Use the schtasks command, or with the query parameter, schtasks /query to query the system's currently executing tasks.

C:\Users\Administrator>schtasks

Folder: \
Task NameNext Run Time Mode=========================================== ===================== =================
Adobe Acrobat Update Task 2019/9/2 11:00:00 Ready SogouImeMgr N/A Ready sync-opensource 2019/9/2 11:30:00 Ready WpsUpdateTask_Administrator 2019/9/2 9:23:46 Ready...

3. Create a scheduled execution task

Type schtasks /create /? in the command line to view a more detailed parameter description. Only a few parameters that we are most concerned about are listed.

/TN taskname Specifies a string in the form of path\name that uniquely identifies this scheduled task.
/TR taskrun Specifies the path and file name of the program to be run at this scheduled time.
 For example: C:\windows\system32\calc.exe
/SC schedule Specifies the schedule frequency.
 ==> Create a scheduled task "EventLog" to start running wevtvwr.msc
 SCHTASKS /Create /TN EventLog /TR wevtvwr.msc /SC ONEVENT
 Such as every minute, every hour, every day, every week MINUTE: 1 to 1439 minutes;
 HOURLY: 1 - 23 hours;
 DAILY: 1 to 365 days;
 WEEKLY: 1 to 52 weeks;
/ST starttime Specifies the start time to run the task.
 The time format is HH:mm (24 hour time), for example 14:30 means 2:30 PM. If /ST is not specified, the default is the current time. /SC ONCE This option is required.

3.1 Create a task

We create a file called "sync-opensource " to periodically execute a bat script at 11:30 every day. The command to create the task is as follows.

schtasks /create /tn "sync-opensource" /tr "E:\PycharmProjects\opensource\sync.bat" /sc daily /st 11:30

4 Others

4.1 Find the specified task

For example, find the sync-opensource task we created above.

C:\Users\Administrator>schtasks -query | find "sync-opensource"
sync-opensource 2019/9/2 11:30:00 Ready

4.2 Deleting a task

You can use the following command to delete a specified task.

schtasks /delete /tr taskname

Summarize

The above is the method of setting up scheduled tasks under Linux and Windows introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Use crontab command in Linux environment to set up scheduled periodic execution tasks [including PHP execution code]
  • Detailed explanation of crontab scheduled execution command under Linux
  • Detailed explanation of at and crontab commands for scheduled execution of tasks in Linux
  • How to execute tasks regularly under Linux and instructions on how to use crontab (collected and sorted)
  • How to use cron to execute tasks regularly in Linux

<<:  How to completely uninstall mysql under CentOS

>>:  Vue implements countdown between specified dates

Recommend

Steps to deploy ingress-nginx on k8s

Table of contents Preface 1. Deployment and Confi...

CSS multi-level menu implementation code

This is a pretty cool feature that makes web page...

HTML page native VIDEO tag hides the download button function

When writing a web project, I encountered an intr...

JavaScript implements H5 gold coin function (example code)

Today I made a Spring Festival gold coin red enve...

Implementation and usage scenarios of JS anti-shake throttling function

Table of contents 1. What is Function Anti-shake?...

Let's talk about bitwise operations in React source code in detail

Table of contents Preface Several common bit oper...

Hbase Getting Started

1. HBase Overview 1.1 What is HBase HBase is a No...

Teach you about react routing in five minutes

Table of contents What is Routing Basic use of pu...

CentOS 6 uses Docker to deploy redis master-slave database operation example

This article describes how to use docker to deplo...

How to deploy Go web applications using Docker

Table of contents Why do we need Docker? Docker d...

Bootstrap 3.0 learning notes button style

This article mainly explains the style of buttons...

64-bit CentOs7 source code installation mysql-5.6.35 process sharing

First install the dependent packages to avoid pro...