LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-28-2011, 02:03 PM   #1
uperate_toor
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Rep: Reputation: Disabled
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.

Last edited by uperate_toor; 09-28-2011 at 02:07 PM.
 
Old 09-28-2011, 02:09 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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)
 
Old 09-28-2011, 02:20 PM   #3
kerrylinux
LQ Newbie
 
Registered: May 2009
Location: Co. Kerry, Ireland
Distribution: CentOS, Fedora, RedHat, Ubuntu Enterprise Cloud
Posts: 12

Rep: Reputation: Disabled
Quote:
Originally Posted by uperate_toor View Post
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.

Last edited by Tinkster; 10-02-2011 at 02:30 PM.
 
Old 09-28-2011, 02:34 PM   #4
uperate_toor
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
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.
 
Old 09-28-2011, 06:29 PM   #5
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
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 &
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
New to linux and have a question about a shell script I'm attempting to write trace_effect Linux - Newbie 9 12-11-2009 02:23 AM
Linux Shell programming question ducka Programming 4 09-01-2003 10:24 AM
linux question about bash, shell ?? albina Linux - General 3 05-15-2002 02:07 AM


All times are GMT -5. The time now is 07:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration