3

I'm trying to detect an error and restart server from django application. I'm using the following code:

try:
 # do something
except:
 print('here')
 subprocess.call(['/home/my_username/restart.sh'])

restart.sh is as follows

#!/bin/sh
/home/my_username/webapps/app/apache2/bin/restart
/home/my_username/webapps/my_db/bin/cron

I'm using webfaction as hosting provider. Aboved code prints statement, but doesn't restart the server and doesn't start mysql database which is under my_db.

Maybe I need to supply username/pass? How to do that?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Paul R
  • 2,631
  • 3
  • 38
  • 72
  • 1
    Does this answer your question? [Tilde (~) isn't working in subprocess.Popen()](https://stackoverflow.com/questions/40662821/tilde-isnt-working-in-subprocess-popen) – ShadowRanger Jan 03 '23 at 18:45
  • I duped to a newer question, because this one has been edited since posting in a way that invalidated it as a good dupe target. – ShadowRanger Jan 03 '23 at 18:46

1 Answers1

10

The subprocess.call won't expand the ~ tilde character to your home directory, and therefore it'll look into your current working directory for a folder called ~ and then from there look for your app

You can use the os.path.expanduser function to get the desired behaviour, it'll be something like this:

try:
 # do something
except:
 print('here')
 subprocess.call([os.path.expanduser('~/webapps/app/apache2/bin/restart')])

This script will look for /home/user/webapps/app/apache2/bin/restart and execute it.

Good luck ;)

Joshua Grigonis
  • 748
  • 5
  • 18