树莓派定时任务crontab

本文作者: 峡州仙士

本文链接: https://cjh0613.gitee.io/blog/202003321crontab.html

版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

操作

进入crontab设置

(sudo) crontab -e

(sudo) 指以何用户身份运行
实际是打开存放设置的文件

其中排列格式为:

使用(sudo) crontab -e不用添加用户

crontab格式
#  m    h    dom    mon    dow    (user)    command
#  分    时    日    月      周    (用户)    命令
#
#       m:表示分钟1~59 每分钟用*或者 */1表示
#       h:表示小时1~23(0表示0点)
#     dom:表示日期1~31
#     mon:表示月份1~12
#     dow:标识号星期0~6(0表示星期天)
#    user:表示执行命令的用户
# command:表示要执行的命令
#
#   * 代表任意数值

设置重启命令

每天凌晨一点重启树莓派
00 01    * * *   root     /sbin/shutdown -r now

间隔时间任务

* */1 * * * /usr/local/etc/rc.d/lighttpd restart
#每一小时重启apache

* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
#晚上11点到早上7点之间,每隔一小时重启apache

运行定时任务

重启操作系统即可

参考资料

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:

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:

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.

# m h  dom mon dow   command
# * * * * *  command to execute
# ┬ ┬ ┬ ┬ ┬
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)

For example:

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:

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:

@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:

@reboot python /home/pi/myscript.py &

and & at the end of the line, like this:

@reboot python /home/pi/myscript.py &

树莓派设置定时重启

树莓派设置定时重启

树莓派 liunx 定时启动与crontab命令格式与详细例子

树莓派 liunx 定时启动与crontab命令格式与详细例子

你可能感兴趣的:(树莓派定时任务crontab)