1

I am developing a backup script in my NAS running a docker container. Inside my container I have cron running with this /etc/crontab:

root@backups-nas:/code# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

0 * * * * root /code/backup.sh

I have also installed syslog-ng, so I can look for logs in /var/log/syslog, where my task appears:

root@backups-nas:/code# tail -f /var/log/syslog 
==> /var/log/syslog <==
Mar 24 19:00:01 backups-nas CRON[9250]: (root) CMD (/code/backup.sh)

The content of my script is this:

root@backups-nas:/code# cat backup.sh
#!/bin/bash
python3 /code/backup.py all >> /code/log/$(date +\%d-\%m-\%Y).log

But when it's o'clock no new log writter in /code/log/$(date +\%d-\%m-\%Y).log and the task is not completed. If I try to execute manually the line of /etc/crontab it works.

What do you think is happening? Thanks in advance

EDIT:

I edited backup.py, starting like this:

#!/usr/local/bin/python3

from sys import argv
import datetime

If I run the script directly works, but cron doesn't run it, although the line in /var/log/syslog.

3
  • What's the path to the python3 interpreter on your system? – Kusalananda Mar 24 '20 at 19:19
  • @Kusalananda The path is: /usr/local/bin/python3 – Ángel Carlos del Pozo Muela Mar 24 '20 at 19:32
  • You're trying to use python3 as the interpreter for backup.sh -- something that looks like (and whose filename suggests is) a shell script? – Andy Dalton Mar 24 '20 at 22:41
0

In your system crontab file, you set the value of the PATH variable to /sbin:/bin:/usr/sbin:/usr/bin. This does not include the path to your python3 interpreter, which you say is located at /usr/local/bin/python3.

This means that you will need to do one of the following things:

  • Add /usr/local/bin to PATH. You can do that in the crontab file by modifying the PATH= line (this would affect all jobs in the system's crontab file), or in the backup.sh script with PATH=$PATH:/usr/local/bin, before calling your Python backup script.
  • Use the Python interpreter with an explicit path when you call your Python script, as in

    /usr/local/bin/python3 /code/backup.py all >> "/code/log/$(date +%d-%m-%Y).log"
    

    (note that the % characters do not have to be escaped here).

  • Rely on the interpreter specified in the #!-line of the /code/backup.py script by not specifying an explicit interpreter at all:

    /code/backup.py all >> "/code/log/$(date +%d-%m-%Y).log"
    

    This relies on the first line of the backup.py script being #!/usr/local/bin/python3, and that the script is executable (chmod +x /code/backup.py).

Out of these variants, the "best" (personal opinion) is the last variation.

4
  • I have just edited my post, please check – Ángel Carlos del Pozo Muela Mar 24 '20 at 22:31
  • @ÁngelCarlosdelPozoMuela Yeah, I said to make sure that the Python script had that #!-line, not the shell script that you call from the crontab. The shell script already had the correct #!-line (calling bash). – Kusalananda Mar 24 '20 at 22:54
  • True! Just edited again the post with no success. – Ángel Carlos del Pozo Muela Mar 24 '20 at 23:13
  • @ÁngelCarlosdelPozoMuela And both the shell script and the Python script are executable? – Kusalananda Mar 24 '20 at 23:44
0

Just solved! I could debug it adding a log in tmp not inside my .sh, in my /etc/crontab.

The problem was simple: I was using a relative path instead of an absolute. But the echo inside my .sh didn't show anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.