使用CRONTAB提交CRON作业时出现问题

2024-10-02 14:24:16 发布

您现在位置:Python中文网/ 问答频道 /正文

问题

我试图使用crontab提交cron作业,但cron正在为我的用户名和根用户执行crontab。bash脚本未执行。但是,如果我手动执行runparts,bash脚本将执行。真奇怪

我不知道我在哪里犯了错误,也不知道如何改正

如果您有什么需要,请告诉我,我会尽力为您获取

设立

bash文件(回显消息)

#!/bin/bash

# below gives the same results as the commented out code
python /home/frosty/code/test_scripts/test.py

# script_dir=/home/frosty/code/test_scripts
# cd ${script_dir}
# python test.py

python文件(test.py)

from datetime import datetime

def main():
    dt_now = datetime.now()
    string_now = dt_now.strftime('%Y-%m-%d %H:%M:%S.%f')
    with open('./text_file.txt', 'a') as f:
        f.write(f'wrote at {string_now}\n')
    return None

if __name__ == '__main__':
    main()

用户crontab(frosty)

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr//sbin:/usr/bin:/sbin:/bin
MAILTO=""

#0,5,10,15,20,25,30,35,40,45,50,55 * * * * frosty /home/frosty/code/c19_refresh_token_controller/scripts/c19_rtc.sh >/dev/null 2>&1
#*/5 * * * * /home/frosty/code/c19_refresh_token_controller/scripts/echo_message_sh >/dev/null 2>&1
*/5 * * * * /home/frosty/code/test_scripts/echo_message_sh

cron.allow文件(/etc)

frosty

anacron crontab(/etc)

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

#SHELL=/bin/sh
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""

# m h dom mon dow user  command
41 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

anacron试验

*** run-parts --test /etc/cron.hourly
/etc/cron.hourly/echo_message_sh

*** ls -la /etc/cron.hourly
total 16
drwxr-xr-x  2 root root 4096 May 24 02:37 .
drwxr-xr-x 97 root root 4096 May 24 08:02 ..
-rw-r--r--  1 root root  102 Nov 16  2017 .placeholder
-rwxrwxrwx  1 root root   86 May 24 04:11 echo_message_sh

cron事件日志(/var/log/syslog)

运行cron作业时日志的一部分

May 24 04:00:01 brentrd CRON[3520]: (frosty) CMD (/home/frosty/code/test_scripts/echo_message_sh)
May 24 04:05:01 brentrd CRON[3633]: (frosty) CMD (/home/frosty/code/test_scripts/echo_message_sh)
May 24 04:10:01 brentrd CRON[3748]: (frosty) CMD (/home/frosty/code/test_scripts/echo_message_sh)
May 24 04:15:01 brentrd CRON[3906]: (frosty) CMD (/home/frosty/code/test_scripts/echo_message_sh)
May 24 04:17:01 brentrd CRON[3962]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 24 04:17:01 brentrd CRON[3961]: (CRON) info (No MTA installed, discarding output)
May 24 04:20:01 brentrd CRON[4107]: (frosty) CMD (/home/frosty/code/test_scripts/echo_message_sh)
May 24 04:25:01 brentrd CRON[4235]: (frosty) CMD (/home/frosty/code/test_scripts/echo_message_sh)
May 24 04:30:01 brentrd CRON[4375]: (frosty) CMD (/home/frosty/code/test_scripts/echo_message_sh)
May 24 04:30:01 brentrd CRON[4374]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 24 04:30:01 brentrd CRON[4372]: (CRON) info (No MTA installed, discarding output)
May 24 04:35:01 brentrd CRON[4452]: (frosty) CMD (/home/frosty/code/test_scripts/echo_message_sh)
May 24 04:40:01 brentrd CRON[4517]: (frosty) CMD (/home/frosty/code/test_scripts/echo_message_sh)
May 24 04:41:01 brentrd CRON[4534]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May 24 04:45:01 brentrd CRON[4567]: (frosty) CMD (/home/frosty/code/test_scripts/echo_message_sh)

结果

手动运行run-parts

run-parts -v --report /etc/cron.hourly

文本文件输出

文件中没有任何cron运行。下面的结果是我运行上面这行时得到的

wrote at 2021-05-24 04:14:31.973873
wrote at 2021-05-24 04:34:48.047038

参考资料

tutorial for crontab
crontab documentation


Tags: runtestechomessagehomeshscriptsetc