在CentOS系统中,你可以使用cron
来设置定时任务,并通过修改crontab
文件来指定执行任务的用户。以下是具体步骤:
打开终端。
输入crontab -e
命令来编辑当前用户的crontab
文件。如果你想为其他用户设置定时任务,请使用sudo crontab -u username -e
命令,其中username
是目标用户的用户名。
在打开的crontab
文件中,你可以按照cron
的时间格式来添加定时任务。时间格式如下:
* * * * * command-to-be-executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday to Saturday; 7 is also Sunday)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
user1
用户在每天的12:00执行/home/user1/backup.sh
脚本,你可以这样写:0 12 * * * user1 /home/user1/backup.sh
crontab
文件。现在,定时任务已经设置好了,并且会以指定的用户身份执行。如果你想查看当前用户的crontab
任务列表,可以使用crontab -l
命令。如果你想删除当前用户的所有crontab
任务,可以使用crontab -r
命令。