Script to run non-stop

Hi All,

I am on a Solaris OS and i have come up with a csh script named " mycshscript " which will grab data from a datalog file & format the grabbed data & upload formated version to web server. I would want to have this script to run non-stop so that the latest can be captured since data is always appending to the datalog file. Below is a skeleton of my script using a while loop to loop non-stop.

My question is that
1) Does closing my command console stop the running of my script?
2) If i want to stop my script from running, how am i able to do it in a clean manner?

#!/bin/csh

set num = 1

while ($num == 1)

......
.......

end

Normally.

The traditional solution is

nohup myprogram &

and run it from a shell without job control.

This will

(a) run the program in the background
(b) ignore any SIGHUP generated by the terminal "hanging up".

Hi Porter,

1) How do i stop the process if i need to?
2) how do i need to create a while loop for this ?
3) If there's an echo command, will it be printed out ?
4) Do i need to add in the " nohup myprogram & >/dev/null 2>&1 " just like cronjob to remove all the echoing and printing command ?

Not necessarily.

I could do the same with ' ksh / zsh ' - which has a good job control.

Would that create any difference ?

get the process id and use kill

already there is a while in your script

use re-direction operators to redirect both stderr and stdout to log-file

thats a better way of doing it.

[quote=matrixmadhan;302144018]
get the process id and use kill

already there is a while in your script

use re-direction operators to redirect both stderr and stdout to log-file

1) How do i get the process id and kill it ? What's the command?

2) I see. Thanks

3) What if there's no redirection operators ? What will happen ?

4) Can i use this ?
nohup myprogram & >/dev/null 2>&1

Run it from a shell that when you try to exit the shell doesn't refuse because it complains you still have jobs running.

man nohup, typically end up with a file called "nohup.out".

Sure, as always, give it a go, see what it does, try and break it.

I agree, that would not hinder the running process.

Its more like a warning and that could be very much ignored.

Hi Porter/matrixmadhan,

I dare not experiment with this command as it is very new to me and i hope that the system will not collapse because of this if not everybody will start to scream at me. So i will have to really make sure everything is ok before i try out.

Can the below command stop the process manually?

ps -aef
kill -9 pid ?

Try with a small dummy script such as

#!/bin/sh
while true
do
    sleep 1000
done

Have a couple of terminal sessions open, run it from one, monitor it from both using "ps" then close the terminal you used to launch it.

You need to gain confidence in what it will and won't do.

Hi Porter,

I tried your dummy script.
I tired to kill the process using kill -9 11417 but I can't kill the process.
Help me pls!!

nohup testing &

After executing ps -aef

user 11417     1  0 16:19:26 ?        0:00 sleep 1000

Are you sure of the process id of the nohup process that is kicked off ?

What is the return status after issuing kill command ?

check it with >> echo $?

How did you confirm that kill command is not working ?

I think you killed the shell running the script :slight_smile: that's good.

but what you are seeing is the "sleep 1000" program is still running, it's parent has died hence it being reparented to the init process (pid=1)

Does "kill -9 11417" return an error?

Hi Porter,

I tried several time before the process can be killed. It will return a " Killed " if successful
The kill command did not return anything at my 1st try.
I tried out serveral time but somehow the process canot be killed during my 1st try.

Yeah indeed the PID and PPID changed to another number!!
But what is the reason for reparenting to the init process (pid=1) ??

Hi Porter,

Seems like i have to perform the following before the " sleep 1000 " goes away. Why is that so ?

1) kill -9 PID
2) kill -9 PPID
3) kill -9 new_PID

How about not using "kill -9" all the time, it is far too aggressive.

Try

kill pid

or

kill -INT pid

or

kill -QUIT pid

only use -9 as an absolute last resort, it gives the process no opportunity to clear itself up.

hi poter,

Tried all 3. It doesn't go away immediately.

Have to perform the below before the sleep goes away.
But why does the PID and PPID change everytime i execute the kill command ?

1) kill -9 PID
2) kill -9 PPID
3) kill -9 new_PID

I've had a play on Solaris and yes the script is very reluctant to die!

if you kill the script process itself with "-9" it will die but leave the sleep running. Eventually the sleep will timeout and it will die.

When a processes parent (in the case of sleep, it is the shell running the script) it gets reparented to 1.

Hi Porter,

Do you mean if i actually perform a non-stop while loop with my csh script, there is a chance where this process cannot be killed?!?
This is very dangerous.
Any other ways to kill the process ?

"kill -9 pid" WILL kill the process if you are the (a) process owner or (b) root. It's just not the most polite way of doing things.