linux scheduled task executes .sh script

1: Create a new directory (it can be your current project, if the execution result of the .sh script is related to your project. If it is related to the system itself, it is recommended to put it in another location)

2: Create a test.sh file in the newly created directory. The content is as follows ( here is to start the mysql service as an example --> close in advance: if you are in a development environment, you can try this: if you are in a production environment, don't do this, In the production environment, you can replace service mysqld start with other commands )

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

export PATH

service mysqld start

3: Execute chmod u+x test.sh --> here is to give the file owner execute permission

4:crontab -e                                                --> here is the list of open scheduled tasks

5: Write the following content into the scheduled task list

*/1 * * * *   /alidata/www/C9/test.sh -->      /alidata/www/C9 is where you create the directory in the first step

6: Restart the scheduled task service crond restart

7: After a minute, use service mysqld status to find that the mysql service is open. This means that your scheduled task execution script is successful.

Note: If mysql is not opened normally, it may be caused by environment variables (the second and third lines of the test.sh file in this article are to solve this problem ). Of course, if you try to use the scheduled task and find that the script is not executed, here you can Manually add the directory where the file is located to the PATH environment variable. In addition, it should be noted that there may be other reasons for unsuccessful execution, which can be solved by viewing the crond log.

                                                                                        Please indicate the source




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324685501&siteId=291194637