4

I know there have been plenty of questions about this, but I've been trying things for a while with no luck. I have a simple python testscript that creates a folder with a timestamped name. It works perfectly when I run it manually, but nothing happens when I try to put it into crontab. Any idea where I'm messing up here? Here's the script: (located in /home/ec2-user/cronscripts)

from subprocess import call
from time import time
import math

call(["mkdir","derp" +str(math.floor(time()))])

and crontab -l produces the output

* * * * * python /home/ec2-user/cronscripts/testscript.py

I've tried putting 'root' into the command as well, but no dice. I've also tried some to mess around with explicitly setting the PATH variable in the script file.

From root@domU-12-31-38-00-AC-02.compute-1.internal  Wed Jun 15 19:57:01 2011
Return-Path: <root@domU-12-31-38-00-AC-02.compute-1.internal>
Received: from domU-12-31-38-00-AC-02.compute-1.internal (localhost [127.0.0.1])
        by domU-12-31-38-00-AC-02.compute-1.internal (8.14.4/8.14.4) with ESMTP id p5FJv1aS006094
        for <root@domU-12-31-38-00-AC-02.compute-1.internal>; Wed, 15 Jun 2011 19:57:01 GMT
Received: (from root@localhost)
        by domU-12-31-38-00-AC-02.compute-1.internal (8.14.4/8.14.4/Submit) id p5FJv1Dc006093;
        Wed, 15 Jun 2011 19:57:01 GMT
Date: Wed, 15 Jun 2011 19:57:01 GMT
Message-Id: <201106151957.p5FJv1Dc006093@domU-12-31-38-00-AC-02.compute-1.internal>
From: root@domU-12-31-38-00-AC-02.compute-1.internal (Cron Daemon)
To: root@domU-12-31-38-00-AC-02.compute-1.internal
Subject: Cron <root@domU-12-31-38-00-AC-02> usr/bin/python /home/ec2-user/cronscripts/testscript.py
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
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>

/bin/sh: usr/bin/python: No such file or directory

This was the last entry I found in the root mail. It seems to not be able to find python, even though it's exactly where that say it is. And when I print crontab -l, usr/bin/python does have a leading slash. Do I need to change a cron path somewhere?

jww
  • 97,681
  • 90
  • 411
  • 885
cfrederich
  • 1,790
  • 6
  • 26
  • 35
  • 1
    look in the system logs to see the errors – hvgotcodes Jun 15 '11 at 19:21
  • Jun 15 18:02:01 domU-12-31-38-00-AC-02 CROND[5310]: (root) CMD (/home/ec2-user/cronscripts/testscript.py) Jun 15 18:03:01 domU-12-31-38-00-AC-02 CROND[5317]: (root) CMD (/home/ec2-user/cronscripts/testscript.py) – cfrederich Jun 15 '11 at 19:31
  • i found this in the cron log, so its clearly trying to do something – cfrederich Jun 15 '11 at 19:31
  • 1
    make sure root's email is setup to go somewhere and have a look at any emails this is generating. If you don't have any mail forwarding enabled run the `mail` command as the root user. – James C Jun 15 '11 at 19:48
  • Seeing `usr/bin/python` as the error message suggests that somewhere along the line, you've lost a leading slash. Maybe you have `#!usr/bin/python` in a shebang? Very often, the problem with "it works on the command line but not in `cron`" comes down to 'environment' — see (amongst others) [Perl script works but not via `cron`](http://stackoverflow.com/questions/4341324/perl-script-works-but-not-via-cron/4341351#4341351). – Jonathan Leffler May 11 '14 at 19:24
  • *+1* for including details like `PATH`. – jww Nov 17 '19 at 18:48

1 Answers1

6

Use fully qualified path for all commands in crontab.

0 */2 * * * /full/path/to/python /home/ec2-user/cronscripts/testscript.py

Also, all fields are *? When do you expect this to be run? The above example would run every other hr.

amit_g
  • 30,880
  • 8
  • 61
  • 118
  • 1
    `The above example would run every hr` Nope. It will run every minute. You need to put some concrete value for the minutes field. And writing `*/1` in any crontab field is the same as writing `*` – leonbloy Jun 15 '11 at 19:24
  • yeah I just wanted to have it run every minute, just so I could see if it was working at all without having to wait – cfrederich Jun 15 '11 at 19:27