树莓派定时任务crontab
树莓派定时任务crontab,2020.6.12添加:Github Action
也可以按这个格式设置定时运行。
操作
进入crontab设置
1 | (sudo) crontab -e |
(sudo) 指以何用户身份运行
实际是打开存放设置的文件
其中排列格式为:
使用(sudo) crontab -e
不用添加用户
1 | # m h dom mon dow (user) command |
设置重启命令
1 | 每天凌晨一点重启树莓派 |
间隔时间任务
Github Action验证过的:
1 | 0 */1 * * * /usr/local/etc/rc.d/lighttpd restart |
另外一种格式:
1 | 0 */1 * * * /usr/local/etc/rc.d/lighttpd restart |
运行定时任务
重启操作系统即可
参考资料
Scheduling tasks with Cron
此摘自树莓派文档
Cron is a tool for configuring scheduled tasks on Unix systems. It is used to schedule commands or scripts to run periodically and at fixed intervals. Tasks range from backing up the user’s home folders every day at midnight, to logging CPU information every hour.
The command crontab
(cron table) is used to edit the list of scheduled tasks in operation, and is done on a per-user basis; each user (including root
) has their own crontab
.
Cron GUI
A graphical application for Cron is available by installing the gnome-schedule
package:
1 | sudo apt install gnome-schedule |
You can then launch the program Scheduled Tasks from the main menu.
Editing crontab
Run crontab
with the -e
flag to edit the cron table:
1 | crontab -e |
Select an editor
The first time you run crontab
you’ll be prompted to select an editor; if you are not sure which one to use, choose nano
by pressing Enter
.
Add a scheduled task
The layout for a cron entry is made up of six components: minute, hour, day of month, month of year, day of week, and the command to be executed.
1 | # m h dom mon dow command |
1 | # * * * * * command to execute |
For example:
1 | 0 0 * * * /home/pi/backup.sh |
This cron entry would run the backup.sh
script every day at midnight.
View scheduled tasks
View your currently saved scheduled tasks with:
1 | crontab -l |
Run a task on reboot
To run a command every time the Raspberry Pi starts up, write @reboot
instead of the time and date. For example:
1 | @reboot python /home/pi/myscript.py |
This will run your Python script every time the Raspberry Pi reboots. If you want your command to be run in the background while the Raspberry Pi continues starting up, add a space and &
at the end of the line, like this:
1 | @reboot python /home/pi/myscript.py & |
and &
at the end of the line, like this:
1 | @reboot python /home/pi/myscript.py & |