1

I try to set up a cron job to execute a python test program (rm a file from folder) but it doesn't work. I've tried different things : - Run programm with script sh - Run action directly with command in crontab

When I launch test.sh from terminal, it works perfectly. When I launch rm file directly in crontab command it works too BUT nothing happens when the script sh is launched from crontab...

This is my cron tab :

*/5 * * * * run_auto_resp_ads.sh
37 14 * * * test.sh
18 14 * * * rm ~/Rendu/test_cron/lol.py

This is test.sh content :

#!/bin/sh rm ~/Rendu/test_cron/lol2.py

When I check my cron logs, task are running :

Mar 15 14:21:01 AcerA17 CROND[14905]: (mjz) CMD (test.sh)
Mar 15 14:18:01 AcerA17 CROND[12944]: (mjz) CMD (rm ~/Rendu/test_cron/lol.py)

I've also checked files rights.

Any ideas please ? Thx a lot :)

Magali Jz
  • 13
  • 6
  • 1
    Use absolute paths for everything in cron. – 123 Mar 15 '18 at 14:55
  • Or, better, put `PATH=/bin:/usr/bin:/usr/local/bin` up at the top of your crontab, which will export that variable for all jobs it runs. Likewise, `HOME=/Users/magali` or such as appropriate. – Charles Duffy Mar 15 '18 at 15:00
  • BTW, `sh` and `bash` are two different shells. Using one in your title and the other in your tagging is confusing at best. – Charles Duffy Mar 15 '18 at 15:01

2 Answers2

0

Cron doesn't inherit your environement. $HOME is not set when you run your cron, thus the ~ is not interpoled.

You can set it like this:

HOME="/home/user"
18 14 * * * rm ~/Rendu/test_cron/lol.py
smaftoul
  • 2,375
  • 17
  • 14
0

Does test.sh consist of one single line? In that case, there you have part of the problem: the construct

#!/bin/sh

needs to stand alone on the first line. In your case "rm" will be sent as an argument to 'sh', which is not what you want.

Arndt Jonasson
  • 856
  • 1
  • 6
  • 14