Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess doesn't execute if the path contains home directory tilde ~

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?

like image 612
Andrew Fount Avatar asked Oct 24 '25 23:10

Andrew Fount


1 Answers

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 ;)

like image 118
Oscar David Arbeláez Avatar answered Oct 26 '25 12:10

Oscar David Arbeláez