Operating System - HP-UX
1834814 Members
2326 Online
110070 Solutions
New Discussion

Running a shell script continuously after exiting the login session

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Running a shell script continuously after exiting the login session

Dear Sir,

i am runnig a shell script test1.sh and recording its out in a file test2. I used the command $./test1.sh >>test2 &

But when i exited xterm session (login session) the script stopped. I wanted to run it continuously even if i logged out. How to do it ?

Thanks,
Shiv
12 REPLIES 12
Joseph Loo
Honored Contributor

Re: Running a shell script continuously after exiting the login session

hi,

because u r running it off as a job at the background, either u do not exit/logoff or setup as a cron job.

regards.
what you do not see does not mean you should not believe
Jeff_Traigle
Honored Contributor
Solution

Re: Running a shell script continuously after exiting the login session

You need to use nohup:

nohup command $./test1.sh >>test2 &

This will create a nohup.out file in the directory you execute it in.
--
Jeff Traigle
Senthil Prabu.S_1
Trusted Contributor

Re: Running a shell script continuously after exiting the login session

Hi Shiva,
use nohup. It will continue to operate, even when u close your session. It will send the output to the file name nohup.out. Even you can specify your own file, where the output can be stored.

EX:
nohup test1.sh &


HTH,
Prabu.S
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Arunvijai_4
Honored Contributor

Re: Running a shell script continuously after exiting the login session

Hi Shiv,

"nohup" is the command that will help you.
nohup - run a command immune to hangups

You can run as,

$ nohup ./test1.sh >>test2 &

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: Running a shell script continuously after exiting the login session

When you start a script it will take the shell base process ID will the uppper level Parent Process ID.

You can avoid that by starting with su as,

# su - -c "/test1.sh 1>/tmp/test1.log 2>&1 &"

Example:
# cat /tmp/muthu/test1.sh
echo "script is started"
sleep 10
echo "script is end"
hostname
ls -l
# su - root -c "/tmp/muthu/test1.sh 1>/tmp/muthu/test.log 2>&1 &"

it is working after log out too.

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: Running a shell script continuously after exiting the login session

Shiv, "nohup" is better way of executing your shell scripts. You dont need to "su" and execute it.

You can see more information at,

http://www.computerhope.com/unix/unohup.htm
http://www.livefirelabs.com/unix_tip_trick_shell_script/june_2003/06022003.htm

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: Running a shell script continuously after exiting the login session

we can use su too. Don't worry. Try to find whether solution is working or not ;)

nohup is another way to achive it. It is like it will execute the process until it is getting completed.

Su is like it will create another process which will not be controlled by currently logged shell.

--
Muthu
Easy to suggest when don't know about the problem!
Senthil Prabu.S_1
Trusted Contributor

Re: Running a shell script continuously after exiting the login session

Muthu,

And it is not advised to use "SU" while running a script. It may lead to a security lapse..:-)


--
Prabu.S
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Arunvijai_4
Honored Contributor

Re: Running a shell script continuously after exiting the login session

I would say, "nohup" is the primay way. If it doesn't help you by any changes, you can go for "su". But, remember this is a big security hole in a production environment and could lead to hazards. Secuirty is the prime in today's world .

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: Running a shell script continuously after exiting the login session

prabhu,

If you are running as a same user then it is not a problem to do right? :)

Like

user -> su to same user.

It is a thought to reach the destination. Dream yourself to get more ways. Don't using same way always ;)

==

Shiv,

Simple way is to use nohup. You can go with su or as well as telnet + piping() program too.

--
Muthu
Easy to suggest when don't know about the problem!
Arturo Galbiati
Esteemed Contributor

Re: Running a shell script continuously after exiting the login session

Hi Shiv,
you have to use nohup:
nohup ./test1.sh >>test2 2>&1 &

I suggest you to redircet the stderr (2) to the stdout (1) otherwise you will find it in the nohup.out file.

HTH,
Art
Steve Andrews_6
Frequent Advisor

Re: Running a shell script continuously after exiting the login session

Hi Shiv:

I would also use the fully qualified path when using nohup.
$nohup /dir/dir/test1.sh >>test2 2>&1 &
In God We Trust