0

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

References

tutorial for crontab
crontab documentation

Brent
  • 196
  • 7
  • Please add your cronjob to your question (no comment, no link, no image). – Cyrus May 24 '21 at 06:45
  • Add output of `which python` to your question (no comment, no link, no image). – Cyrus May 24 '21 at 06:49
  • Use `/full/path/to/python` in your script `echo_message_sh`. – Cyrus May 24 '21 at 06:51
  • @Cyrus; that doesn't work. I get the same results. – Brent May 24 '21 at 07:03
  • The Bash wrapper is completely superfluous. You can simply put `python /path/to/script` in your `crontab` – tripleee May 24 '21 at 07:46
  • "No MTA installed, discarding output" means it is generating an error message, but it has nowhere to deliver it. Please review [Cron job not running](https://stackoverflow.com/questions/22743548/cronjob-not-running) for troubleshooting steps. – tripleee May 24 '21 at 07:47
  • @tripleee Do I need to add a particular shebang on the first line of the python file? something like #!/usr/bin/python. – Brent May 24 '21 at 08:08
  • The shebang is strictly necessary only if you don't spell out `python` in front of the script's name. – tripleee May 24 '21 at 08:10
  • @tripleee thanks I really appreciate it. I have one other question. If I have multiple cron jobs that need to run in different conda environments. In general it's not a good idea to have one huge conda environment to run the scripts in. I like to use bash files to source different conda environments. That way I can create a bash file to run different scripts. Am I thinking about this wrong since you said a bash file is not useful for cron jobs? – Brent May 24 '21 at 08:13
  • A file which contains a single command is useless because you might as well spell out that command directly. If you want to run something more complex in the shell, by all means put that in a file. – tripleee May 24 '21 at 08:17
  • @tripleee I understand the lack of utility of one line in a bash file. The point if to understand by cron is not executing the bash file. I don't have an answer to that on and the references provide haven't helped. I don't mean to upset you but this doesn't help me. I have several complex bash files to run data science and API scripts. This is a simple example illustrating the problem. My question isn't answered and I don't think the question should be closed. My opinion. – Brent May 24 '21 at 08:24
  • The actual problem is almost certainly that the file you are trying to read is not in your home directory, which is where `cron` runs your job. The troubleshooting tips clearly explain how to capture standard error by various means so that you can see the error message. Each of these in isolation is a common FAQ, and combined this is arguably too broad. – tripleee May 24 '21 at 08:34
  • @tripleee The issue was not that the file was not in my home directory. Cron was using the wrong python interpreter, probably python 2. The code was written to use python 3.9. I would appreciate it if you could open this question so I could write the answer. You have to source the correct conda environment. If you want I can reword the question. But the problem wasn't what you thought. – Brent May 24 '21 at 09:10
  • Probably still a duplicate, but I'll be happy to add a duplicate pointer to a question about that specific problem if you can find one. – tripleee May 24 '21 at 09:12
  • 1
    @tripleee I will do some searching. I will provide you with my results. – Brent May 24 '21 at 09:14
  • @tripleee it looks like the text interpreter found a similar question or you add it. I will post my answer to that one. Thanks for your help. – Brent May 24 '21 at 09:19

0 Answers0