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

MySQL 8.0.12 installation configuration method and password change

This article records the installation and configu...

Use Smart CSS to apply styles based on the user's scroll position

By adding the current scroll offset to the attrib...

Detailed explanation of MySql slow query analysis and opening slow query log

I have also been researching MySQL performance op...

Four completely different experiences in Apple Watch interaction design revealed

Today is still a case of Watch app design. I love...

How to verify whether MySQL is installed successfully

After MySQL is installed, you can verify whether ...

Detailed explanation of Angular routing sub-routes

Table of contents 1. Sub-route syntax 2. Examples...

How to install Docker on Windows 10 Home Edition

I recently used Docker to upgrade a project. I ha...

Detailed explanation of angular content projection

Table of contents Single content projection Multi...

Linux's fastest text search tool ripgrep (the best alternative to grep)

Preface Speaking of text search tools, everyone m...

HTML tags explained

HTML tags explained 1. HTML tags Tag: !DOCTYPE De...

Solution to CSS flex-basis text overflow problem

The insignificant flex-basis has caused a lot of ...

Using an image as a label, the for attribute does not work in IE

For example: Copy code The code is as follows: <...

Xftp download and installation tutorial (graphic tutorial)

If you want to transfer files between Windows and...

MySQL 5.7.23 installation and configuration graphic tutorial

This article records the detailed installation pr...