Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

386
Views
Calling to restart a "service" in Linux from Python script not working from within crontab entry

I have the following python test script (extracted from a much bigger .py file) which I'm trying to get working on an EC2 instance (haproxy OpsWorks instance).

There seems to be a major difference between the permissions applied when running as a cron entry versus running as a python script from the shell.

The closest I've found in any other posts is here but that's relating to shell scripts and, given that I'm running python OK, the paths should ( I hope ) be sufficiently configured for the cronjob.

Environment: - Python version: 2.6.8 - Environment: OpsWorks HAProxy instance, on EC2. - Running as user: root.

Script

import subprocess
import boto
import logging

if __name__ == '__main__':
    logging.getLogger().setLevel(boto.logging.INFO)
    command = ['service', 'haproxy', 'reload'];
    logging.info('Executing: %s' % command)
    #shell=FALSE for sudo to work.
    subprocess.call(command, shell=False)

I have alternatively tried calling the subprocess command using the following to no avail.

# subprocess.call("%s %s %s %s" % ('sudo', 'service', 'haproxy', 'reload'))

Output when run from the command line:

[root@lb1 ~]# python tester.py
INFO:root:Executing: ['service', 'haproxy', 'reload']
Reloading haproxy:
[root@lb1 ~]#

Crontab entry:

[root@lb1 ~]# crontab -l
*/1 * * * * python ~/tester.py > ~/testlog 2>&1

Output when run as a crontab command

[root@lb1 ~]# cat testlog
INFO:root:Executing: ['service', 'haproxy', 'reload']
Traceback (most recent call last):
  File "/root/tester.py", line 13, in <module>
    subprocess.call(command, shell=False)
  File "/usr/lib64/python2.6/subprocess.py", line 478, in call
    p = Popen(*popenargs, **kwargs)
  File "/usr/lib64/python2.6/subprocess.py", line 639, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.6/subprocess.py", line 1228, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

I'm confused as to why on earth it's throwing the "No Such File Or Directory" error!

Can anyone provide any pointers? Incidentally this is part of a much bigger EC2 integration piece - hence the boto import [it's a nice way to get logging in this instance]. It's running as root because, from browsing SO, it's bad practice to have a cron entry for a user account running with a sudo command.

Cheers,

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

In subprocess set shell=True

subprocess.call(command, shell=True)

I think your problem will be solved but sudo problem may be arised.

over 4 years ago · Santiago Trujillo Report

0

It turns out that it is indeed path related.

Path Information for Cronjobs
Commands in submodules

To solve it I provided the following entries via crontab -e

[root@lb1 ~]# crontab -l
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Now my service restarts as expected. Yay!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!