Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a python script from shell script cron

I have a shell script cron which calls a python script from same directory but when this cron is executing, i am not getting expected output from my python script and when i execute it manually my python script's output is as expected.

I have provided python script paths like /usr/bin/python room_wise.py

and given all shell params in shell script as well but still my python script is not called using the shell script cron.

Can anyone help me here?

like image 297
Sid Avatar asked Apr 07 '26 12:04

Sid


1 Answers

The big issue in cron jobs is the absolute directory locations and relative directory locations. You need to split the relative path out firstly as shown.

#!/usr/bin/env bash
dirName=`dirname $0`
baseName=`basename $0`
arg1=$1
arg2=$2
cd ${dirName} && python ./room_wise.py arg1 arg2

Then use crontab -e to add items to your user cron jobs and add the following:

PATH=/usr/bin:/bin:/sbin
30 00 * * * /my/directory/containing/room_wise_py.sh arg1 arg2 > /my/directory/containing/output.log 2>&1

You can see that I've added the PATH since this can sometimes be a problem with certain Operating system distributions. Also, the script exists in the same directory as the bash script, or you can pass the directory location as an argument if you modify the bash script to include dirname as $1.

Also you can see that I've directed all output to a log file. This is a really good idea since its sometimes very difficult to debug the process if something goes wrong.

like image 96
Eamonn Kenny Avatar answered Apr 10 '26 01:04

Eamonn Kenny