Run Script With nohup Command On A Different Server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run Script With nohup Command On A Different Server
# 1  
Old 05-22-2009
Run Script With nohup Command On A Different Server

Hi,
I want to run a particular script present in a different server.
At the moment I am trying to run it like this:
Code:
(sleep 3; echo $USERID; sleep 1; echo $PASSWORD ; sleep 1 ; sleep 1 ;
echo "nohup perl myscript.pl $* &")| telnet "$server"

But the problem is that once the script has typed in the command to run the script it is coming out of the telnet session.
I say this because if I put in a sleep time like below:

Code:
(sleep 3; echo $USERID; sleep 1; echo $PASSWORD ; sleep 1 ; sleep 1 ;
 echo "nohup perl myscript.pl $* &; sleep 100")| telnet "$server"

then the script runs properly.
Now my problem is that I cant put in a hard coded value for this sleep time.
Depending upon the situation, the script will take varying amount of time for its completion.
This is quite a soup that I have landed myself in Smilie.
Thanks in advance Smilie
# 2  
Old 05-22-2009
Judging from this command
echo "nohup perl myscript.pl $* &"
it seems that your approach is "fire and forget".
Perl runs in background with all the output redirected to a file nohup.out, so there is no point in keeping the telnet session open.

Otherwise, if you want to wait for perl to be done then the command should be like
echo "perl myscript.pl $*"
and you should wait (read) for some output that signals the end.
To read from the other process (telnet) a pipe is not enough, you have to use coroutines.

I once made a korn shell script to send an email using telnet, perhaps you can glean something from it:
Code:
#!/usr/bin/ksh
 
# -----------------------
# CONFIG BEGIN
 
SMTPHOST=whatever.wherever.com
MAILFROM=xxx.yyy@wherever.com
MAILRECIPIENTS="abc.def@wherever.com
ghi.jkl@wherever.com
"
 
# CONFIG END
# ---------------------
 
function sendemail {
 
 function waitfor {
   typeset expected=$1 answer
   while read -p answer
   do
     print -- "$answer"
     [[ $answer = $expected\ * ]] && return
   done
 }
 
 function expect {
   typeset expected=$1 answer
   read -p answer
   print -- "$answer"
   [[ $answer = $expected\ * ]] || return 1 # return error if answer is not as expected
 }
 
 typeset subject="$1"
 typeset body="$(sed 's/^\.$/../')"
 typeset recipient
 
 trap 'print "Cannot send email"; return 1' ERR
 
 telnet $SMTPHOST 25 |&
 
 waitfor 220
 print -p HELO hereiam.wherever.com
 expect 250
 print -p "MAIL FROM:<${MAILFROM}>"
 expect 250
 for recipient in $MAILRECIPIENTS
 do
   print -p "RCPT TO:<$recipient>"
   expect 250
 done
 print -p DATA
 expect 354
 print -n -p "From: $SCRIPTNAME <${MAILFROM}>\nTo: "
 for recipient in $MAILRECIPIENTS
 do
   print -n -p "$recipient,"
 done
 print -p "\nSubject: $subject\n$body\n."
 expect 250
 print -p QUIT
}
 
# ---------------------
SCRIPTNAME=${0##*/}
 
date "+Good morning, today is %d/%m/%Y and the time is %H:%M:%S." |
sendemail "Greetings from $SCRIPTNAME"

# 3  
Old 05-22-2009
why don't you use ssh, command will be something like below:

ssh <IP> "nohup perl myscript.pl $* "

it will come out from that session once the script is finished.

Regards,
Kiran
# 4  
Old 05-22-2009
Thanks for all ur help Colemar.
Ideally what I would want is for the perl script to run there and then. You said that I need to get a signal from nohup in order to understand that the script is done with its execution. But how am I supposed to do so?
I also removed the & at the end so that it runs in the foreground only. But I could not see any file being created for nohup.out or any file that the called perl script should have created ...

Sorry, bit of a numb skull here Smilie ...
# 5  
Old 05-22-2009
ssh is not an option on these servers Smilie ...
# 6  
Old 05-22-2009
Quote:
Originally Posted by King Nothing
Ideally what I would want is for the perl script to run there and then.
Please clarify, what would you like to do?

Quote:
Originally Posted by King Nothing
You said that I need to get a signal from nohup in order to understand that the script is done with its execution. But how am I supposed to do so?
Not from nohup, you don't want to use nohup nor & when waiting for the perl process to end.
The perl script should output something on the standard output when its job is done; if it is completely silent, at least the remote shell should output a prompt. If you were to telnet manually you would see that output on your terminal screen. Since your shell script is using telnet, not you, it is your script that has to read from the two-way pipe (|&) and decide if and when the perl script is finished.

Quote:
Originally Posted by King Nothing
I also removed the & at the end so that it runs in the foreground only. But I could not see any file being created for nohup.out or any file that the called perl script should have created ...
Of course the file nohup.out is created on the remote machine, and more precisely in the current directory for the nohup process, and that directory happens to be the home dir of $USERID (since I see no cd commands).
# 7  
Old 05-22-2009
I tried without nohup and & altogether. But still the script did not execute for some reason completely unknown to me.
I can try to read something in the shell script to determine that the perl script has executed if it runs at the 1st place.
I wish ssh was there.

nohup.out was not created in the directory from where the perl script was called when I had used nohup without the sleep time. I just get this feeling that the script just hangs up when ever the telnet session is disconnected.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nohup apending data each time i run script

I have a problem here. i am running my script in nohup but if i run it 2 or three time , in my output i see it is giving me old data as well, in-spite i delete nohup.out file. i tried to look a lot from where the old data is coming. can some one tell me how the old data is also coming again and... (11 Replies)
Discussion started by: mirwasim
11 Replies

2. 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

3. Shell Programming and Scripting

Script for telnet and run one command kill it and run another command using while loop

( sleep 3 echo ${LOGIN} sleep 2 echo ${PSWD} sleep 2 while read line do echo "$line" PID=$? sleep 2 kill -9 $PID done < temp sleep 5 echo "exit" ) | telnet ${HOST} while is executing only command and exits. (5 Replies)
Discussion started by: sooda
5 Replies

4. Shell Programming and Scripting

Need script to run the command in remote server

hi, I need script to perform below task. 1. Read the IP address 2. copy the script from origin server to destination. 3. get root access on destination server 4. run the script on destination server 5. return to the origin server Code: #!/bin/bash echo "Enter Server IP... (5 Replies)
Discussion started by: bapu1981
5 Replies

5. Shell Programming and Scripting

run shell script under nohup directly [solved]

Hi, i am not able to run the loop in nohup directly. nohup 'for i in $(seq 10); do echo $i;./mscript.sh $i; done' can some one help me how to run this directly in nohup? ---------- Post updated 03-15-12 at 12:20 AM ---------- Previous update was 03-14-12 at 11:59 PM ---------- From... (0 Replies)
Discussion started by: johninweb
0 Replies

6. Shell Programming and Scripting

Need help on how to exit a script run on a server from a remote server

hi, I am using the below line to run a script from remote server(say server A) to another server(say server B). ssh username@servername ksh script name. The issue is the script logs into server B, executes the script on server B, transfers the file to server A but does not exit from... (4 Replies)
Discussion started by: yohasini
4 Replies

7. UNIX for Dummies Questions & Answers

How to run a command on server

Hi, Im a total virgin on server admin and I need to check server (shared server) for certain files. Someone send me a command: find /home/ \( -name "*.cgi" -o -name "*.php" \) -print0 | xargs -0 egrep -l 'c99shell|r57shell|WebShell|phpshell|shell|c100|base64' >> /root/report my question is:... (1 Reply)
Discussion started by: Carl29
1 Replies

8. Shell Programming and Scripting

nohup command in the script....

I want to know how to use a nohup command in the script........ I have to run following command nohup /tmp/app/statuscheck.sh & After typing this command I will type ctrl D to come to the prompt and the that command will run in backround. Even after pressing & the command is not... (3 Replies)
Discussion started by: jayaramanit
3 Replies

9. Shell Programming and Scripting

nohup command in a script.......

I want to know how to use a nohup command in the script........ I have to run following command nohup /tmp/app/statuscheck.sh & After typing this command I will type ctrl D to come to the prompt and the that command will run in backround. I want to include that command in a... (2 Replies)
Discussion started by: jayaramanit
2 Replies

10. Solaris

I want to run a script or command on other server

Hi all, I have done ssh-keygen to two servers in work place and given there entry for authorized_keys. I m able to ssh to other servers without asking password. But i face problem while trying to run a command or script on other server. It is throwing an Error. $ ssh... (4 Replies)
Discussion started by: naree
4 Replies
Login or Register to Ask a Question