2

I'm just setting up a cron tab/job on my Cent OS developement server.

Within my crontab I have the following. (Ignore the time setting, this was added about 15:32 UTC server time just to get the next scheduled run in).

34 15 * * * cd welcomeclient-0.0.5 && python3.6 main.py

In the command line cd welcomeclient-0.0.5 && python3.6 main.py works fine. welcomeclient-0.0.5 is under root in the droplet, and python3.6 is in /usr/bin.

Any suggestions?

2 Answers 2

2

Try using absolute paths in your crontab command:

34 15 * * * cd /foo/bar/welcomeclient-0.0.5 && /usr/bin/python3.6 main.py

or, assuming the main.py does not also make use of relative paths inside of it :

34 15 * * */usr/bin/python3.6 /foo/bar/welcomeclient-0.0.5/main.py
Sign up to request clarification or add additional context in comments.

2 Comments

does that make a difference to how I should reference it?
What I've amended it to is this: 50 20 * * * /usr/bin/python3.6 /root/welcomeclient-0.0.5/main.py && >> /root/welcomeclient-0.0.5/main.log
1

Looks like you're trying to change directory in crontab, just like omu_negru said you'll need to use full path instead. That's because crontab even if running under your name will not inherit your environmental variables such as $PATH.

Try this. First, go to the directory where your script is... and turn your main.py to an executable file so you don't have to call python main.py anymore. The simplest way to do so is...

$ chmod u+x main.py

Now if you do ls -l you'll see that you have "x" in the user section of permissions which will allow you to run it directly.

-rwxr--r-- 1 user user 0 Aug 17 17:55 main.py

Now you're ready to simplify crontab syntax to something like this...

34 15 * * * /foo/bar/welcomeclient-0.0.5/main.py 

I also like to capture the output from the script to a log file so it's easier to troubleshoot when things don't work our as planned, as follows:

34 15 * * * /foo/bar/welcomeclient-0.0.5/main.py & >> /foo/bar/main.log 

The log should be added to log rotation, otherwise it will keep filling up and eventually make your system run out of space, but that's another topic already addressed on this site.

2 Comments

Added info included in my question - also, does main.log have to exist before anything is output or will the cron create this after the first "successfull-but-not-successful" run?
I am actually now 100% sure. I typically go to the directory and touch main.log just to make sure it does exist before things are logged to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.