MeanDumpsterCat
Posts: 3
Joined: Thu Nov 17, 2016 5:11 pm

Python subprocess not running under cron

Thu Nov 17, 2016 5:20 pm

Hello everybody. I'm new on the forum here. I am currently building a twitter bot. This bot tweets the output of the fortune program. I have everything working but im having problems getting it to schedule in cron. At the moment I have it set to run every 1 minute. My script is stored in /home/pi.

When I added it to crontab I am getting the following message in /var/mail/pi

Code: Select all

From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <[email protected]> /usr/bin/python /home/pi/tweet.py
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Cron-Env: <PATH=/usr/lib/python2.7:/usr/bin/python2.7>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/pi>
X-Cron-Env: <LOGNAME=pi>
Message-Id: <[email protected]>
Date: Thu, 17 Nov 2016 17:06:03 +0000

Traceback (most recent call last):
  File "/home/pi/tweet.py", line 6, in <module>
    proc = subprocess.Popen("fortune", stdout=subprocess.PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 20] Not a directory

My crontab is as follows

Code: Select all

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
PATH=/usr/lib/python2.7:/usr/bin/python2.7
*/1 * * * * /usr/bin/python /home/pi/tweet.py
And finally, here is my python script. The Twitter keys have been removed for this post but this script works perfectly when I run 'python /home/pi/tweet.py'

Code: Select all

import tweepy
import sys
import subprocess
import os

proc = subprocess.Popen("fortune", stdout=subprocess.PIPE)
fortune = proc.stdout.read()


# Consumer keys and access tokens, used for OAuth  
consumer_key = 'XXXXXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXXX'
access_token = 'XXXXXXXXXXXXXXX'
access_token_secret = 'XXXXXXXXXXXXXXXXX'

# OAuth process, using the keys and tokens  
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Creation of the actual interface, using authentication  
api = tweepy.API(auth)

#api.update_status('Test tweet please ignor')

while len(fortune) < 135:
        api.update_status(status=fortune)
        print "//Fortune Tweet posted//"
        sys.exit(0)

if fortune > 135:
        print "//The generated mesasge was greater than the character limit//"
        print "//restarting script//"
        os.system('python tweet.py')
        sys.exit(0)



Any help would be much appreciated. Have a good day!

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python subprocess not running under cron

Thu Nov 17, 2016 6:54 pm

Try adding a full path to "fortune".
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python subprocess not running under cron

Thu Nov 17, 2016 7:03 pm

You also seem to rerun the entire script if the fortune is too long. I think that's unnecessary. You could call fortune again and again until you get one that is the right length.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

MeanDumpsterCat
Posts: 3
Joined: Thu Nov 17, 2016 5:11 pm

Re: Python subprocess not running under cron

Thu Nov 17, 2016 7:06 pm

elParaguayo wrote:Try adding a full path to "fortune".
Oh wow that worked! Thank you.
For those reading this my crontab now looks like this

Code: Select all

# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
PATH=/usr/lib/python2.7:/usr/bin/python2.7:/usr/games
*/15 * * * * /usr/bin/python /home/pi/tweet.py


MeanDumpsterCat
Posts: 3
Joined: Thu Nov 17, 2016 5:11 pm

Re: Python subprocess not running under cron

Thu Nov 17, 2016 7:07 pm

elParaguayo wrote:You also seem to rerun the entire script if the fortune is too long. I think that's unnecessary. You could call fortune again and again until you get one that is the right length.
And yes I was thinking that. Im very new to python so I still have much to learn. This is still very much a work in progress

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python subprocess not running under cron

Thu Nov 17, 2016 11:01 pm

No problem. If you need any more help then just let us know.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

guilleso
Posts: 1
Joined: Thu Apr 19, 2018 4:23 am

Re: Python subprocess not running under cron

Thu Apr 19, 2018 4:29 am

elParaguayo wrote:
Thu Nov 17, 2016 6:54 pm
Try adding a full path to "fortune".
Hola, necesito ayuda con crontab

necesito que se ejecute un archivo de python, el cual contiene:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
subprocess.call(['shutdown', '-h', 'now'], shell=False)

la idea es que al ejecutarse, se apague mi raspberry, pero no lo logro hacer...
si lo ejecuto a mano desde la consola, funciona perfectamente... pero no desde el cron

supongo que el problema es de permisos del crontab o algo por el estilo. uso: sudo crontab -e

desde ya muchas gracias

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Python subprocess not running under cron

Thu Apr 19, 2018 11:30 am

Please post in English if possible.

Can you post the line from your crontab.

Also, you probably want the full path to shutdown. I don't remember if it's /usr/bin/shutdown or /usr/sbin/shutdown.

EDIT: I think it's actually /sbin/shutdown
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”