Problem
I'm trying to submit a cron job using crontab but cron is executing the crontab for my user name and root. The bash script is not executing. However, if I execute run-parts manually the bash script executes. It's really weird.
I don't know where I made my mistake or how to fix it.
if you anything let me know and I will do my best to get it for you
Set-up
bash file (echo_message_sh)
#!/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 file (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()
user 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 file (/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 test
*** 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 events log (/var/log/syslog)
part of the log when the cron jobs were run.
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)
Results
manually run run-parts
run-parts -v --report /etc/cron.hourly
text file output
none of the cron runs are in the file. the below results are when I ran the line above.
wrote at 2021-05-24 04:14:31.973873
wrote at 2021-05-24 04:34:48.047038