crontab 脚本默认是root用户运行,如果是普通用户(非root), 比如leag ,而定时任务又是root用户执行的,,会导致生成的日志文件属于root,进而导致leag用户无权限写入日志报错。下面记录一下,使用普通用户执行定时任务的方法。
1、crontab 常用的命令
crontab -u //指定某个用户的cron服务
crontab -l //列出某个用户的cron服务的详细内容
crontab -r //删除某个用户的cron服务
crontab -e //编辑某个用户的cron服务
2、查看crontab 定时任务的方法:
cat /etc/crontab
root@localhost:/home/ap/LeagView/Log/python>cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
3、Linux 下crond定时任务使用普通用户执行脚本的方法
crontab -e默认创建的用户是root用户,创建普通用户的定时执行债务,执行下面命令:
# 如果要创建普通leag用户的定时任务:
crontab -u leag -e //编辑leag 用户的命令
# 使用下面命令查看leag用户下的定时任务内容:
crontab -u leag -l //查看leag 用户的命令列表
还有一种方法,就是将原来的root的定时任务的名字改为普通用户leag的名字,重启crond服务,定时任务就能以普通用户leag运行了