Implementation of linux timing tasks

Sometimes we need to delete some redundant log files on the server. Use the linux command.
First, we must know how to write the linux delete command to
find the corresponding directory -mtime + number of days -name "file name" -exec rm -rf {} \ ;#正号Is one day ago, minus sign is within one day

find /root/log/ -mtime +1 -name "*.log" -exec rm -rf {
    
    } \;
#含义将/root/log/目录下所有1天前带".log"的文件删除

The above is to manually type the command, and write the command to the sh file. The
first step

touch   /root/delete_one_day.sh#创建文件,
chmod +x delete_one_day.sh#赋予可执行权限

Second step

vim delete_one_day.sh
#!/bin/sh
find /root/log/ -mtime +1 -name "*.log" -exec rm -rf {
    
    } \;

Save and exit (:wq)
scheduled task

crontab -e
*/5 * * * * sh /root/delete_one_day.sh#每五分钟执行这个.sh文件

Scenario application

There is a py program under the monitoring root directory. If the program suddenly hangs due to some special reason, please restart it.

Idea:
By finding the process of changing the program, you can determine whether to hang up ps -ef |grep xx.py |grep -v "grep" |wc -l If the
result is 1, the program is normal, otherwise the program has been terminated.
Step 1: Write monitor .sh file

#!/bin/sh
count=`ps -ef |grep xx.py |grep -v "grep" |wc -l`
echo $count
if [ 0 == $count ];then
nohup python  /xx/xx.py > /xx/xx.log 2>&1 &
echo $count
fi

The second step is to set up a scheduled task

 crontab -e

Time-sharing day month week

          • sh /monitor.sh means that the script monitor.sh is executed every minute

Guess you like

Origin blog.csdn.net/qq_34237321/article/details/102837159