# Crontab - Python run problem



## ekoo (Dec 14, 2013)

Hello, I have added a Python script to crontab. The script does not work from crontab (only through manual operation), and I receive a message through the mail:


```
From root@xxxxxx.hosted.by.stagnom.com Sat Dec 14 07:09:00 2013
Return-Path: <root@xxxxxx.hosted.by.stagnom.com>
Received: from xxxxxx.hosted.by.stagnom.com (localhost [127.0.0.1])
	by xxxxxx.hosted.by.stagnom.com (8.14.5/8.14.5) with ESMTP id rBE790ZY001131
	for <root@xxxxxx.hosted.by.stagnom.com>; Sat, 14 Dec 2013 07:09:00 GMT
	(envelope-from root@xxxxxx.hosted.by.stagnom.com)
Received: (from root@localhost)
	by xxxxxx.hosted.by.stagnom.com (8.14.5/8.14.5/Submit) id xxxxxxxx;
	Sat, 14 Dec 2013 07:09:00 GMT
	(envelope-from root)
Date: Sat, 14 Dec 2013 07:09:00 GMT
Message-Id: <201312140709.xxxxxxxx@xxxxxx.hosted.by.stagnom.com>
From: root@xxxxxx.hosted.by.stagnom.com (Cron Daemon)
To: root@xxxxxx.hosted.by.stagnom.com
Subject: Cron <root@xxxxxx> cd /usr/path && python script.py
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>

python: not found
```


----------



## wblock@ (Dec 14, 2013)

Short answer: The Interrupted Unix FAQ, #2.

Longer answer: those email headers show the cron environment.  Is python on that path?  No.  Some suggest modifying the path in the crontab, but I prefer using full paths as the more portable solution.


----------



## dave (Sep 23, 2016)

I know this is an age-old thread, but it's relevant - partly because this page came up on a Google search for: crontab "python: not found"

I am running /usr/local/bin/gcloud (the command line Google Cloud Engine utility) via crontab.  It is coded in Python, I believe.  When I try to run a cron script that calls /usr/local/bin/gcloud, I get this error:


```
/usr/local/bin/gcloud: python: not found
```
I am following The Interrupted Unix FAQ, #2, but the program I am calling is having path problems.

What is the elegant/correct way to solve this?


----------



## SirDice (Sep 23, 2016)

In your case setting PATH in the crontab(1) or at the top of your own script would be the easiest and cleanest solution.


----------



## dave (Sep 23, 2016)

In my script, I added


```
echo "PATH: ${PATH}"
```
And the output I got was:


```
PATH: /sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/home/myusername/bin:/home/myusername/Scripts:/home/myusername/.rvm/bin
```
Also...


```
ls -lah l /usr/local/bin/python
ls: l: No such file or directory
lrwxr-xr-x  1 root  wheel     7B 17 Jun 18:23 /usr/local/bin/python -> python2
```

Now I wonder - why is python not found?

I should also note that this is a script run from my own crontab, not root.


----------



## SirDice (Sep 23, 2016)

dave said:


> And the output I got was:
> 
> 
> ```
> ...


Did you get that path running the script yourself or from crontab(1)?


----------



## dave (Sep 23, 2016)

Riiiight.  The output when run from crontab(1) is:


```
PATH: /usr/bin:/bin
```

So I will update and report back...


----------

