[tool] Ubuntu、Win10 设置开机启动python脚本

前言:

话说我每次设置的服务器,再次开机,ip都会随之改变,固定ip有时候确定不好用。所以为啥不让让每次启动都发送ip给我呢。

步骤:

sudo touch /etc/rc.local
sudo chmod 777 /etc/rc.local
sudo systemctl enable rc-local.service
vim /etc/rc.local 
#编写脚本
    #!/bin/bash -e 
    /bin/su - username -c "/home/username/start.sh"
        #nohup python3 /home/xxxxx/.py >./log.log 2>&1&

    echo start xx.py
    exit 0
    

sudo systemctl start rc-local.service #启动

sudo systemctl status rc-local.service #查看
输出 >: start xx.py
sudo systemctl stop rc-local.service #关闭

Win10:

1. 新建一个pythonstart.bat文件,内容格式如下:

cd /d d:

cd workspace
python main.py

pause

2. 将pythonstart.bat文件放入开机自启动文件夹:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

双击文件,看是否正常运行。

重启电脑后,系统会自动打开一个cmd窗口运行pythonstart.bat中的内容,

若关闭当前窗口,python服务也将停止运行。

定期清理日志

cp /dev/null /home/.../nohup.out

进入定时任务

crontab -e

crontab每分钟定时执行:

*/1 * * * * service mysqld restart //每隔1分钟执行一次
*/10 * * * * service mysqld restart //每隔10分钟执行一次
1
2
crontab每小时定时执行:

0 */1 * * * service mysqld restart //每1小时执行一次
0 */2 * * * service mysqld restart //每2小时执行一次
1
2
crontab每天定时执行:

0 10 * * * service mysqld restart //每天10点执行
30 19 * * * service mysqld restart //每天19点30分执行
1
2
crontab每周定时执行:

0 10 * * 1 service mysqld restart //每周一10点执行
30 17 * * 5 service mysqld restart //每周五17点30分执行
1
2
crontab每年定时执行:

0 10 1 10 * service mysqld restart //每年的10月1日10点执行
0 20 8 8 * service mysqld restart //每年的8月8日20点执行
 

结论:

再搭配上发送到qq邮件的代码,简直美滋滋。

猜你喜欢

转载自blog.csdn.net/qq_55542491/article/details/132172710