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

Sample code for implementing menu permission control in Vue

When people are working on a backend management s...

Example of implementing todo application with Vue

background First of all, I would like to state th...

Display mode of elements in CSS

In CSS, element tags are divided into two categor...

Using better-scroll component in Vue to realize horizontal scrolling function

About Recently, in the process of learning Vue, I...

Example analysis of the usage of the new json field type in mysql5.7

This article uses an example to illustrate the us...

How to pass the value of the select drop-down box to the id to implement the code

The complete code is as follows : HTML code: Copy ...

How to solve the problem of FileZilla_Server:425 Can't open data connection

When installing FileZilla Server on the server, t...

Docker installs the official Redis image and enables password authentication

Reference: Docker official redis documentation 1....

Zabbix implements monitoring of multiple mysql processes

Three MySQL instance processes are started on one...

Detailed explanation of the pitfalls of mixing npm and cnpm

Table of contents cause reason Introduction to NP...

How to Set Shortcut Icons in Linux

Preface Creating shortcuts in Linux can open appl...

Implementation of MySQL Multi-version Concurrency Control MVCC

Table of contents What is MVCC MVCC Implementatio...

Detailed explanation of how two Node.js processes communicate

Table of contents Preface Communication between t...

100 ways to change the color of an image using CSS (worth collecting)

Preface “When it comes to image processing, we of...