Keep a script on remote machine running (nohup?)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Keep a script on remote machine running (nohup?)
# 1  
Old 05-26-2015
Keep a script on remote machine running (nohup?)

Hi,

I'm using expect to ssh into remote machine (i know its not the best practice), and run script "script.sh".

This "script.sh" checks whether an other process (some another script) is running and if not, it runs it as some other user.

Code:
#!/bin/bash

/usr/bin/expect << EOD
        set timeout 2
        spawn ssh "$user@$host"
        sleep 2
        expect "*assword:"
        send "$passwd\r"
	
        expect ">"
	send "/home/$user/script.sh\r"
        sleep 2
        expect ">"
        send "exit\r"
EOD


Heres the "script.sh"
Code:
#!/bin/bash
        if [ -z "$(pgrep some_process)" ]; then
		echo "some_process not running.. Starting.."
        	echo "passwd" | su - user
        	nohup ./some_process &
		echo "PS : $(ps ax | grep some process)"
                echo "passwd_of_original_user" | su - original_user
		echo "PS : $(ps ax | grep some process)" #some_process is already not running at this line
        else
 		echo "Some_process already running.."
        fi

Now the problem is that the "some_process" should run continuously, yet at the second check of running processes (the line with a comment in code) it is already not running.

From what I googled, the "nohup" command should keep it running even after logging out of the system, but apparently it doesn't.

I dont know what I'm doing wrong, any help will be appreciated
# 2  
Old 05-26-2015
What does "some process" do? Is it short lived by definition? Any error msgs? Does your grep work correctly (the "_" is missing in the search pattern)? Why do you change from pgrep to grep? I think by switching to and fro between users you're closing your shell/script...
# 3  
Old 05-26-2015
Some_process isnt short lived. It runs continuously until user tells it to stop.

The greps are working correctly (the first one displays the process, I've obviously changed the code to be easier to read for people here, so missing "_" is not a real problem).


As for closing my shell/script - from what I understand that's exactly what
Code:
nohup

is for - keep the process running even after logout, user change, shell close, yet somehow it seems to be not working
# 4  
Old 05-26-2015
Is it really required to have 2 passwords, 1 for each user, hardcoded in the script, that is stored remotely?

AFAIU: expect is to make it easy to the user to TYPE the password which then is passed to 'whatever'.
Why is it even required to change the user?

Further, i'd change:
Code:
nohup ./some_process &
echo "PS : $(ps ax | grep some process)"

to
Code:
nohup ./some_process &
PID=$!
echo "PS : $(ps ax | grep $PID)"

or even just
Code:
nohup ./some_process &
PID=$!
ps $PID|grep -v PID

My 2 cents, hth

Last edited by sea; 05-26-2015 at 07:21 PM..
# 5  
Old 05-26-2015
I don't think the below is the correct way to run some_process as user

Code:
		echo "some_process not running.. Starting.."
        	echo "passwd" | su - user
        	nohup ./some_process &
		echo "PS : $(ps ax | grep some process)"
                echo "passwd_of_original_user" | su - original_user
		echo "PS : $(ps ax | grep some process)" #some_process is already not running at this line

Instead try something like this:
Code:
		echo "some_process not running.. Starting.."
        	echo "passwd" | su - user -c "nohup ./some_process &"
		echo "PS : $(ps ax | grep some process)" #some_process is already not running at this line

However su on many systems will also refuse to accept passwords from a pipe issuing an error like standard in must be a tty.

sudo is usually a better choice in this situation, as the requiretty constraint can be relaxed for some_process or user.
If as I suspect you have little admin control over host you may be forced to run another expect script to get some_process running as user

Last edited by Chubler_XL; 05-26-2015 at 08:19 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 05-27-2015
Thanks Chubler, that code is indeed the correct way as I just checked

Code:
echo "passwd" | su - user -c "nohup ./some_process &"

Although I couldnt check it on the original "some_process" today, it seemed to click with "some_another_process" Smilie I sure hope it'll also work for the original one or I'll get sad
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running nohup command on remote server

I am having an issue in running a nohup command in a remote linux box from a linux box. Here are the details. Linux Machine 1: I have a script which starts a services and dumps the output into a log file. nohup sh weblogic.sh >> /home/log.out & I have placed the entire command in a... (2 Replies)
Discussion started by: accessbalaji
2 Replies

2. OS X (Apple)

Quit a shell script thats running on a remote machine

I'm in a situation where I am executing a shell script(Bash) on another machine remotely using ssh, and for various reasons sometimes need to quit it and restart it. The shell script being run does many different things, so its hard to know what process to kill on the remote machine, and even if I... (2 Replies)
Discussion started by: TheDrizzle
2 Replies

3. Shell Programming and Scripting

Error while running script using nohup

Hi, I am running the below script. When i run the script normally it executes fine but when i run the same script using nohup it throws an error as getStatus.sh: syntax error at line 3: `(' unexpected . Can you let me know why it is so? while do . $(dirname $0)/include.sh cd... (2 Replies)
Discussion started by: vignesh53
2 Replies

4. Shell Programming and Scripting

Running remote system shell script and c binary file from windows machine using java

Hi, I have an shell script program in a remote linux machine which will do some specific monitoring functionality. Also, have some C executables in that machine. From a windows machine, I want to run the shell script program (If possible using java). I tried with SSH for this. but, in... (1 Reply)
Discussion started by: ram.sj
1 Replies

5. Shell Programming and Scripting

Needing guidance, running xargs on remote machine

I am trying to run the follow command. I am running this from a korn shell script. rsh foobar ps -fu dram | grep sshd: | grep -v grep | awk '{print $2}' | xargs -I xx kill xx I get the PID back excluding the xargs part but after adding on the xargs part it fails to locate the PID and is... (2 Replies)
Discussion started by: juredd1
2 Replies

6. Shell Programming and Scripting

check web server running on local and on remote machine

Hi , How to check whether web server is running from remote machine How to check whether web server is running on web server itself Can any one help me soon (1 Reply)
Discussion started by: satheeshkr_cse
1 Replies

7. UNIX for Advanced & Expert Users

Script running on remote machine - How ??

Hi All, This was an interview question " There is a clean-up shell-script in one UNIX machine and it is connected to 100 other UNIX machines. Howe can we run the script on all the 100 machines without ftping/copying the script to target machines ? I was unable to answer, please answer if... (5 Replies)
Discussion started by: coolbhai
5 Replies

8. Shell Programming and Scripting

running script in background on remote machine

Hi there I have a script which is running a remote command on hundreds of boxes, it takes around 5 minutes to return an output from this command and because i am running this all from a central box, it goes off to each box in my for loop sequentially meaning that my script will wait for output... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

9. UNIX for Advanced & Expert Users

Running script on remote machine

if i have a script in my system which i need to run on remote system using ssh, how shall i do it? One easy way to to first scp it to remote machine and then run it on remote machine using ssh. Is there any one step way to do it. Preferably one in which i should give password only once (3 Replies)
Discussion started by: vickylife
3 Replies

10. UNIX for Dummies Questions & Answers

leave a process running in a remote machine

Hi, I would like to run a process in my gentoo machine from a consolte (putty) in Windows and would like that this process keep on going when I close the console in Windows (i.e closing this session). The process should take a long time and I do not want to leave the Windows machine running... (3 Replies)
Discussion started by: pbasil
3 Replies
Login or Register to Ask a Question