1

I'm in Ubuntu linux.

I just added this cron-job to run every minutes

MAILTO="[email protected]"                                                                                                                                                                                    
* * * * *  cd /home/forge/bheng/public/code/scraper/; /usr/bin/python3 main.py

If I run this command

cd /home/forge/bheng/public/code/scraper/; /usr/bin/python3 main.py

It works fine.

I never see it run or get an email.


I ran a mail command

I got

└── mail                                                                                                           
"/var/mail/root": 3 messages 3 new                                                                                 
>N   1 Cron Daemon        Sun Oct 15 06:34  21/930   Cron <root@bheng> test -x /usr/sbin/anacron || ( cd / && run  
 N   2 long@bheng         Sun Oct 15 14:19  15/649   *** SECURITY information for bheng ***                        
 N   3 long@bheng         Sun Oct 15 14:19  15/649   *** SECURITY information for bheng ***                        
?

It seems to have issues, how do I fix it ?


Did I forgot something or missing something?

6
  • I find it easier to wrap the commands in a bash script and tell cron to run the script. Easier control, debugging, bash command interpreter rather than cron's.
    – waltinator
    Commented Dec 21, 2018 at 2:16
  • to exclude path issues, it is generally recommended to use full paths in cronjobs. I'd start by using /usr/bin/python3 /home/forge/bheng/public/code/scraper/main.py instead and see if it works Commented Dec 21, 2018 at 5:17
  • I don't see why there should be a cd; use /usr/bin/python3 /home/forge/bheng/public/code/scraper/main.py instead. Commented Dec 21, 2018 at 7:00
  • I trust you checked your gmail spam folder as well, just in case the message went there?
    – derobert
    Commented Dec 21, 2018 at 9:02
  • 1
    If you change the line to * * * * * cd /home/forge/bheng/public/code/scraper/; /usr/bin/python3 main.py >/tmp/out 2>/tmp/errs, do you see anything in either of those files in /tmp after waiting for a minute? Commented Dec 21, 2018 at 23:53

3 Answers 3

2

If a cron job fails for some reason, you will get a relevant message through mail. Type 'mail' and check the messages.

3
  • When I type mail, I got this : i.imgur.com/NrahSbq.png
    – code-8
    Commented Dec 21, 2018 at 14:42
  • Is that what you mean ? Do I need to install that mail plug in ?
    – code-8
    Commented Dec 21, 2018 at 14:43
  • Do you have any other suggestions ?
    – code-8
    Commented Dec 21, 2018 at 18:39
2

As an alternative to mail, consider wrapping your commands into a bash script and then using a log file:

For example, you could create a script /home/forge/scraper.bash with the following contents:

#!/bin/bash -x
cd /home/forge/bheng/public/code/scraper/
/usr/bin/python3 main.py

Give the script execute permissions (chmod +x scraper.bash), and then set up the cron as follows:

* * * * * /home/forge/scraper.bash &>> /home/forge/scraper.log

The execution output (and errors) should now be available in the /home/forge/scraper.log file. Note that the output is appended to the log file each time the job is run.

0

The MAILTO setting in a crontab file is not generally for Internet email delivery, but rather for local user account delivery, e. g. root or someuser.

3
  • Am I missing something that why my cron is not running ?
    – code-8
    Commented Dec 21, 2018 at 1:20
  • I think it should work find... as long as email is fully configured on the system. Which it may well not be (it often can't be by default), that'd be a good thing to explain in your answer.
    – derobert
    Commented Dec 21, 2018 at 9:02
  • Do you have any other suggestions ?
    – code-8
    Commented Dec 21, 2018 at 18:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .