script not working outside of directory

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1












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







share|improve this question



















  • 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










  • @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 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














up vote
0
down vote

favorite
1












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







share|improve this question



















  • 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










  • @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 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












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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







share|improve this question











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









share|improve this question










share|improve this question




share|improve this question









asked Apr 20 at 16:26









geekluv

11




11











  • 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










  • @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 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
















  • 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










  • @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 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















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















active

oldest

votes











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)