linux + how to Kill stuck program after few seconds - linux

I execute the following binary file on my linux/Solaris system ( in order to get system info )
/usr/sbin/diag
After diag command running,I get some lines on screen , but its stuck , and I not get the Linux/Solaris prompt (diag program not return exe code 0 or 1 because its stuck -:( )
( the only way to exit prom diag is to perform CNTL – C )
my question: if there are some ways to kill the diag binary program after ~5 second
For example
/usr/sbin/diag & ( the lines runs on screen but diag stuck , need to CNTRL-C )
Wait ~5
Kill the /usr/sbin/diag process (&!)

/usr/sbin/diag & # run diag in background
pid=$! # set last run command's process id into pid var
sleep 5s # wait 5 seconds
kill -9 $pid # kill that pid

your shell has job control disabled.
Check that you
are running on a terminal
are starting the shell in interactive mode
See
Shells offer features geared specifically for interactive use rather than to augment the programming language. These interactive features include job control, command line editing, command history and aliases. Each of these features is described in this manual.
And
Job Control

See the timeout command, you should install it, then run
timeout 60 command_line
for a 60 secondes timeout. It's safe and smart.

One possible approach:
/usr/sbin/diag &
sleep 5 ; kill $!
The bash(1) variable $! refers to the pid of the most recently executed backgrounded command. So, if you cannot place diag into the background, then this won't work for you.

timelimit 60 command_line args
Is also a way to do this. It is part of the netpipes package.

Related

Detaching a background process in zsh so that I can close the terminal

I have a terminal process running zsh in the Cygwin environment. I want to start a background process and then close the terminal process and have the background process running. However, when I exit the terminal, I get the error message that it can't exit, because a background process is still running. Example:
sleep 300 &
exit
I get the message zsh: you have running jobs.
The same with
nohup sleep 300 &
From the man page of zsh, I see that there is a command called disown which, from the description, might do what I want, but I don't know how to use it. According to the man page, it expects an argument job, but doesn't say what this argument actually is. I tried
sleep 300 &
disown $!
but get the message disown: job not found: 3964, so a job is obviously not a PID.
How can I do this correctly?
You need to refer to the job by it's job number
sleep 3000
control-z
bg
jobs
disown %3 (or whatever job number)
Hope this still helps at lest other readers: disown without any parameter disconnects the job with job number %1.
In addition you might want to check out the AUTO_CONTINUE on Ctrl-Z function.
alias bg='bg && disown'
in .zshrc to get bash-style behavior exiting terminal that launched process with keystrokes: Ctrl + Z, bg, exit

Run "dummy" background command with specific text

I'm looking for a bash command I can run in the background that will sleep for a bit (60 seconds), and the command will contain a specific text string I can grep out of a ps command.
I can't release a "dummy" script I'm afraid, so it needs to be a one line command.
I tried
echo "textneeded">/dev/null && sleep 60 &
But of course the only text I can grep for is the sleep, as the echo is over in a flash.
(The reasoning for this is it's for putting another script in "test" mode so it doesn't create child processes, but other functionality that ensures there are none of these processes running will still find something, and therefore wait. The other functionality isn't in a bash script.)
I had to do this to test a process killing script. You can use perl to set the process name.
perl -e '$0="textneeded"; sleep 60' &
Original props goes to this guy

Linux stop a command while its loading in PUTTY/SSH

Let me start of by saying i just started learning linux.
I have a command that keeps processing/loading until i press CTRL+C to cancel/stop the command and remain in session.
What i want is to send that command, wait x amount of seconds and then close it programmatically in a single command (cause i can't enter new commands when its loading). Sorry if it sounds stupid, but that would solve my problem.
Would really appreciate if someone could've helped me out here, couldn't find anything on google that was working (mostly exiting the session while the command remains loading).
Use sleep command for terminate the process.
example:
echo helloword & sleep 1 --> time indicates in seconds.After executing the echo command it will wait for one second and then it terminate the echo process
use nohup. example:
nohup ./foobar.sh & < /dev/null > ./log 2>&1
exit
In foobar.sh you can have the following content
command you wish to execute
sleep <no of seconds>
or you can use "screen", you need to install "screen" in you host, then
> screen {your-cmd}
and press ctrl+A followed ctrl+D detach current (but your cmd still running) session .

how can I continously run a unix script in background without using crontab.

how can I continously run a script in background without using crontab. The script should run even after I logout and is acceptable if it doesen't start after system reboot.I am new to unix.
There's a couple of ways of doing this but the neatest way is to use screen. This lets you create a session which lives perminently on the machine and you can simply reconnect to to check on the progress of your long running process.
If you don't wish to use screen you can use nohup this allows you to run a task like:
nohup mytask &
Your task will now run in the background and will survive a log off, however there's no way to take control of it again, unlike with screen.
if [ "x$1" != "x--" ]; then
$0 -- 1> /dev/null 2> /dev/null &
exit 0
fi
This is how you can run a script as a daemon. First your script (the father) will create a copy of himself (a child) so it is considerd as a process of the father. Then the father kills itself while the child is still running. Guess what happens when you do such a thing ? The child is attached to the init process. So even if you logout, the script will still run.
You can even start it without the "&" operator because you start the father which is killed a millisecond after.
You can take control over it again like any program running on your computer.
By the way it's not a real "daemon" program, it's just kind of emulation. You can't just start it at the boot (I mean really the BOOT and not the loggin) if you want to start it as you login, quite simple put it in your .xinitrc
The main advantage of this solution is that your script doesn't depend on any other programm such as "nohup" which is really bad I think.
Regards
PS : If you want some informations about what the command above does, just ask me. It's just a "parameter" thing.
As others have mentioned before, you need to use nohup to prevent the process from getting the hangup signal (hence no-h-up).
However, if you start the process in the background to begin with, as
prompt> nohup process &
that has the disadvantage of not allowing you to enter any data that may be required to get the process started off. This may be passwords/credentials or other input the process needs.
If you have that requirement, start it without the "&" at the end, enter your input and then hit Ctrl-Z to put the process to sleep. To send it to the background, type "bg" at the prompt and hit Enter.
prompt> nohup process
Enter password:
(Now press Ctrl-Z)
[1]+ Stopped process
prompt> bg
[1]+ process &
Now even if you log off, the process will continue to run in the background.
Alternatively if you are using bash or zsh, if you didn't start the process with nohup to begin with, and killing it and restarting is not an option then you can use the built-in disown command. First pause and background the process. And then stop hangup signals from reaching it.
prompt> process
Enter password:
(Now press Ctrl-Z)
[1]+ Stopped process
prompt> bg
[1]+ process &
prompt> disown -h
Note: If you've got other background jobs running, you need to provide the jobspec to only disown this specific job.
prompt> disown -h %1
Instead of [1] if you'd seen [2] when you paused and sent the process to the background, you'd say disown -h %2 instead.
As well as starting it in the background, as above, you may need to use 'nohup'. This means it will carry on running, even if you close the terminal.
nohup ./abc.sh &
start it in background using & operator e.g.
./abc.sh & this will continue till (a) the execution is complete or (b) you kill it or (c) system reboots

Running the shell scripts in Background

I would to give the user the feature to run the shell script in background.
My shell program instantiates a number of other shell scripts.
Here is a small code snippet of my script
./main.sh # Main script
in main.sh
I call preprocessing.sh
create_dir.sh
handle_file.sh
post_processing.sh
report_generation.sh
I would like to know if I have to initiate all the child script as well.. What is the syntax if i have to initiate all the scripts in background and at the end inform the user by displaying message in that test run is complete.
Thanks
Kiran
Start your processes in the background with & and then use bash's builtin wait command:
wait [n ...]
Wait for each specified process and return its termination sta‐
tus. Each n may be a process ID or a job specification; if a
job spec is given, all processes in that job’s pipeline are
waited for.
A couple of example are available here. For instance:
# wait on 2 processes
sleep 10 &
sleep 10 &
wait %1 %2 && echo "Completed!"
add "&" to the end of the commands
I call preprocessing.sh &
create_dir.sh &
...

Resources