Cron Job 文件创建 - 创建的文件权限
Cron Job File Creation - Created File Permissions
我正在 运行 进行每小时一次的 cron 作业进行测试。此作业 运行 是一个名为 "rotateLogs" 的 python 文件。 Cron 不能使用扩展,所以文件的第一行是#!/usr/bin/python。这个 python 文件 (fileA) 然后调用计算机其他地方的另一个 python 文件 (fileB)。 fileB 注销到带有时间戳等的日志文件。但是,当 fileB 运行 通过 fileA 作为 cron 作业时,它会将其日志文件创建为 rw-r--r-- files.
问题是,如果我随后尝试从 fileB 登录文件,除非您 运行 他们具有 sudo 权限,否则他们无法写入文件。所以我正在寻找一些方法来处理这个问题。理想情况下,将文件简单地制作为 rw-rw-r-- 文件会很好,但我不知道如何使用 cron 来做到这一点。感谢您的帮助。
编辑:rotateLogs(故意不是 .py):
#!/usr/bin/python
#rotateLogs
#Calls the rotateLog function in the Communote scripts folder
#Designed to be run as a daily log rotation cron job
import sys,logging
sys.path.append('/home/graeme/Communote/scripts')
import localLogging
localLogging.localLog("Hourly log",logging.error)
print "Hello"
crontab 中没有命令,但它运行在每小时的 cron 上正常运行(在整点过后 17 分钟)。
FileB的相关函数:
def localLog(strToLog,severityLevel):
#Allows other scripts to log easily
#Takes their log request and appends it to the log file
logging.basicConfig(filename=logDirPath+getFileName(currDate),format="%(asctime)s %(message)s")
#Logs strToLog, such as logging.warning(strToLog)
severityLevel(strToLog)
return
我不确定如何找到 cronjob 的 user/group,但它只是在 /etc/cron.hourly 中,我认为它是 root?
事实证明 cron
不提供任何 shell 配置文件(/etc/profile
、~/.bashrc
),因此必须在脚本中设置 umask被 cron 调用。
使用用户级crontabs(crontab -e
)时,umask可以简单设置如下:
0 * * * * umask 002; /path/to/script
即使它是 python 脚本,它也可以工作,因为 os.umask
的默认值继承自 shell 的 umask。
但是,将 python 脚本放在 /etc/cron.hourly
等中,除了 python 脚本本身之外,无法设置 umask:
import os
os.umask(002)
我正在 运行 进行每小时一次的 cron 作业进行测试。此作业 运行 是一个名为 "rotateLogs" 的 python 文件。 Cron 不能使用扩展,所以文件的第一行是#!/usr/bin/python。这个 python 文件 (fileA) 然后调用计算机其他地方的另一个 python 文件 (fileB)。 fileB 注销到带有时间戳等的日志文件。但是,当 fileB 运行 通过 fileA 作为 cron 作业时,它会将其日志文件创建为 rw-r--r-- files.
问题是,如果我随后尝试从 fileB 登录文件,除非您 运行 他们具有 sudo 权限,否则他们无法写入文件。所以我正在寻找一些方法来处理这个问题。理想情况下,将文件简单地制作为 rw-rw-r-- 文件会很好,但我不知道如何使用 cron 来做到这一点。感谢您的帮助。
编辑:rotateLogs(故意不是 .py):
#!/usr/bin/python
#rotateLogs
#Calls the rotateLog function in the Communote scripts folder
#Designed to be run as a daily log rotation cron job
import sys,logging
sys.path.append('/home/graeme/Communote/scripts')
import localLogging
localLogging.localLog("Hourly log",logging.error)
print "Hello"
crontab 中没有命令,但它运行在每小时的 cron 上正常运行(在整点过后 17 分钟)。
FileB的相关函数:
def localLog(strToLog,severityLevel):
#Allows other scripts to log easily
#Takes their log request and appends it to the log file
logging.basicConfig(filename=logDirPath+getFileName(currDate),format="%(asctime)s %(message)s")
#Logs strToLog, such as logging.warning(strToLog)
severityLevel(strToLog)
return
我不确定如何找到 cronjob 的 user/group,但它只是在 /etc/cron.hourly 中,我认为它是 root?
事实证明 cron
不提供任何 shell 配置文件(/etc/profile
、~/.bashrc
),因此必须在脚本中设置 umask被 cron 调用。
使用用户级crontabs(crontab -e
)时,umask可以简单设置如下:
0 * * * * umask 002; /path/to/script
即使它是 python 脚本,它也可以工作,因为 os.umask
的默认值继承自 shell 的 umask。
但是,将 python 脚本放在 /etc/cron.hourly
等中,除了 python 脚本本身之外,无法设置 umask:
import os
os.umask(002)