script not working outside of directory

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a script (activate.sh) that checks for a running python script and, if not found, restarts it
#!/bin/bash
ps auxw | grep my_script | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "Process is not running."
source /path/to/venv/bin/activate #start python environment
export PYTHONUNBUFFERED=yes #to allow for running log output
python /path/to/my/script/my_script.py >> /path/to/my/script/logs/output.log &
fi
This seems to be a pretty basic script template found online for restarting a failed service.
I then set it up on crontab to run every minute.
However, for some reason, the script is only detecting the service (the grep line) if I run it in the local directory that it is in.
If I run /my/path/to/script/activate.sh (from my home directory), nothing
and crontab is not starting the service either
bash grep cron python
add a comment |Â
up vote
0
down vote
favorite
I have a script (activate.sh) that checks for a running python script and, if not found, restarts it
#!/bin/bash
ps auxw | grep my_script | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "Process is not running."
source /path/to/venv/bin/activate #start python environment
export PYTHONUNBUFFERED=yes #to allow for running log output
python /path/to/my/script/my_script.py >> /path/to/my/script/logs/output.log &
fi
This seems to be a pretty basic script template found online for restarting a failed service.
I then set it up on crontab to run every minute.
However, for some reason, the script is only detecting the service (the grep line) if I run it in the local directory that it is in.
If I run /my/path/to/script/activate.sh (from my home directory), nothing
and crontab is not starting the service either
bash grep cron python
Are you sure it'sgrepthat's failing? Can you run the python script from a different path from the command line?
â choroba
Apr 20 at 16:32
compare grep outputs inside and outside the directory to rule it out
â ajeh
Apr 20 at 16:35
@choroba - yes, I can run the python script. It's in this section here: ps auxw | grep my_script | grep -v grep > /dev/null if [ $? != 0 ] then because the 'echo "Process is not running."' doesn't print out
â geekluv
Apr 20 at 16:45
add to the top of the scriptPATH=$PATH:/usr/bin
â Rui F Ribeiro
Apr 20 at 16:49
1
Please usepgrepinstead of trying to get theps|greppipeline working correctly. Does the Python script itself rely on being started with a particular working directory?
â Kusalananda
Apr 20 at 16:55
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a script (activate.sh) that checks for a running python script and, if not found, restarts it
#!/bin/bash
ps auxw | grep my_script | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "Process is not running."
source /path/to/venv/bin/activate #start python environment
export PYTHONUNBUFFERED=yes #to allow for running log output
python /path/to/my/script/my_script.py >> /path/to/my/script/logs/output.log &
fi
This seems to be a pretty basic script template found online for restarting a failed service.
I then set it up on crontab to run every minute.
However, for some reason, the script is only detecting the service (the grep line) if I run it in the local directory that it is in.
If I run /my/path/to/script/activate.sh (from my home directory), nothing
and crontab is not starting the service either
bash grep cron python
I have a script (activate.sh) that checks for a running python script and, if not found, restarts it
#!/bin/bash
ps auxw | grep my_script | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "Process is not running."
source /path/to/venv/bin/activate #start python environment
export PYTHONUNBUFFERED=yes #to allow for running log output
python /path/to/my/script/my_script.py >> /path/to/my/script/logs/output.log &
fi
This seems to be a pretty basic script template found online for restarting a failed service.
I then set it up on crontab to run every minute.
However, for some reason, the script is only detecting the service (the grep line) if I run it in the local directory that it is in.
If I run /my/path/to/script/activate.sh (from my home directory), nothing
and crontab is not starting the service either
bash grep cron python
asked Apr 20 at 16:26
geekluv
11
11
Are you sure it'sgrepthat's failing? Can you run the python script from a different path from the command line?
â choroba
Apr 20 at 16:32
compare grep outputs inside and outside the directory to rule it out
â ajeh
Apr 20 at 16:35
@choroba - yes, I can run the python script. It's in this section here: ps auxw | grep my_script | grep -v grep > /dev/null if [ $? != 0 ] then because the 'echo "Process is not running."' doesn't print out
â geekluv
Apr 20 at 16:45
add to the top of the scriptPATH=$PATH:/usr/bin
â Rui F Ribeiro
Apr 20 at 16:49
1
Please usepgrepinstead of trying to get theps|greppipeline working correctly. Does the Python script itself rely on being started with a particular working directory?
â Kusalananda
Apr 20 at 16:55
add a comment |Â
Are you sure it'sgrepthat's failing? Can you run the python script from a different path from the command line?
â choroba
Apr 20 at 16:32
compare grep outputs inside and outside the directory to rule it out
â ajeh
Apr 20 at 16:35
@choroba - yes, I can run the python script. It's in this section here: ps auxw | grep my_script | grep -v grep > /dev/null if [ $? != 0 ] then because the 'echo "Process is not running."' doesn't print out
â geekluv
Apr 20 at 16:45
add to the top of the scriptPATH=$PATH:/usr/bin
â Rui F Ribeiro
Apr 20 at 16:49
1
Please usepgrepinstead of trying to get theps|greppipeline working correctly. Does the Python script itself rely on being started with a particular working directory?
â Kusalananda
Apr 20 at 16:55
Are you sure it's
grep that's failing? Can you run the python script from a different path from the command line?â choroba
Apr 20 at 16:32
Are you sure it's
grep that's failing? Can you run the python script from a different path from the command line?â choroba
Apr 20 at 16:32
compare grep outputs inside and outside the directory to rule it out
â ajeh
Apr 20 at 16:35
compare grep outputs inside and outside the directory to rule it out
â ajeh
Apr 20 at 16:35
@choroba - yes, I can run the python script. It's in this section here: ps auxw | grep my_script | grep -v grep > /dev/null if [ $? != 0 ] then because the 'echo "Process is not running."' doesn't print out
â geekluv
Apr 20 at 16:45
@choroba - yes, I can run the python script. It's in this section here: ps auxw | grep my_script | grep -v grep > /dev/null if [ $? != 0 ] then because the 'echo "Process is not running."' doesn't print out
â geekluv
Apr 20 at 16:45
add to the top of the script
PATH=$PATH:/usr/binâ Rui F Ribeiro
Apr 20 at 16:49
add to the top of the script
PATH=$PATH:/usr/binâ Rui F Ribeiro
Apr 20 at 16:49
1
1
Please use
pgrep instead of trying to get the ps|grep pipeline working correctly. Does the Python script itself rely on being started with a particular working directory?â Kusalananda
Apr 20 at 16:55
Please use
pgrep instead of trying to get the ps|grep pipeline working correctly. Does the Python script itself rely on being started with a particular working directory?â Kusalananda
Apr 20 at 16:55
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438987%2fscript-not-working-outside-of-directory%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Are you sure it's
grepthat's failing? Can you run the python script from a different path from the command line?â choroba
Apr 20 at 16:32
compare grep outputs inside and outside the directory to rule it out
â ajeh
Apr 20 at 16:35
@choroba - yes, I can run the python script. It's in this section here: ps auxw | grep my_script | grep -v grep > /dev/null if [ $? != 0 ] then because the 'echo "Process is not running."' doesn't print out
â geekluv
Apr 20 at 16:45
add to the top of the script
PATH=$PATH:/usr/binâ Rui F Ribeiro
Apr 20 at 16:49
1
Please use
pgrepinstead of trying to get theps|greppipeline working correctly. Does the Python script itself rely on being started with a particular working directory?â Kusalananda
Apr 20 at 16:55