LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   linux shell question (https://www.linuxquestions.org/questions/linux-newbie-8/linux-shell-question-905460/)

uperate_toor 09-28-2011 01:03 PM

linux shell question
 
Hello, i wrote a python script to check wether servers on my remote VPS machine are online.

When they are not online, it means the process could have died, or that the process is stalled ( not responding). When it's not responding, it won't respond to packet i send with python.

When server is not responsive, i run a .sh script with os.system('script.sh process_name path_to_process').

my script.sh looks like this:

Code:

echo "$1"
echo "$2"

$pid = `pidof -s $1`
if [ $pid -eq ""]
then
  echo "$1 not found!"
else
  echo $pid
  kill $pid
fi
screen "$2"
screen "-d"

basically what i want to do is:
check if the process is alive: if so, kill it.
start server, and detach it from screen and return to python window.

This script gives me all kind of problems:
./home/script.sh: line4 =:command not found
and
There is no screen to be detached.

I'm doing this all using putty on a remote VPS. I have absolutely no idea what i'm doing in the shell script, i just all googled this together. I don't feel like learning ANOTHER scripting language, so that's why i came here to ask.

Nylex 09-28-2011 01:09 PM

Firstly, you don't use the $ sign in assignment statements. Next, there shouldn't be spaces either side of the equals sign. Thirdly, it's more readable if you use $() instead of backticks. So, the offending line should read

Code:

pid=$(pidof -s $1)

kerrylinux 09-28-2011 01:20 PM

Quote:

Originally Posted by uperate_toor (Post 4484637)
This script gives me all kind of problems:
./home/script.sh: line4 =:command not found
and
There is no screen to be detached.

Please remove the blanks on the left and right side of = .

If you intend to see the result of the shell script inside python (you are using echo BTW) you have to call the script in a very different way from python.

Quote:

PYTHON CODE:
Pipe = os.popen("script.sh", "r")
Result = Pipe.read()
Pipe.close()
print Result
You'll need a pipe to read the standard output of the shell script. If you use os.system you could only use commands that don't write to stdout (like cp or killall). IMHO a simple killall would do what you want.

uperate_toor 09-28-2011 01:34 PM

Alright,
now i have os.popen("./home/script.sh" + arg1 +" "+ arg2) in python

and this is the script.sh
Code:

killall "$1"
screen "$2"

problem: doesn't automatically detach from screen, script doesnt exit because it hangs in the screen.

snooly 09-28-2011 05:29 PM

Look at the output of "ps -afe". You'll see two columns PID and PPID. PID is the pid of the process on that line, and PPID is the pid of the parent of that process.

In order to run a process which keeps running when you log out, you might like to use the "nohup" command. If you don't use nohup, when you log out, the process you started will receive the hangup signal SIGHUP, and if it is a well-behaved process, it will probably hangup when it gets that signal. So you log out, the process stops.

If you use "nohup", the process won't get the SIGHUP, so it won't hangup. It will instead change its parent pid (PPID) to be 1, which is probably the init process.

So what you might like to try is:

* start the process in the background with nohup
* log out
* log in again and check that the process is still running and has changed its PPID to 1
* make sure the servers are still running.

something like this might work:

nohup server-process &


All times are GMT -5. The time now is 11:37 AM.