I'm calling another script from my bash script like so -- with an ampersand.
OtherScriptThatDoesNotExit &
echo "done"
I see it getting to "done" but I still see my original script running is ps. Any idea why this could be? (Note, I'm running on Puppy Linux)
The script is still waiting on the subprocess you spawned. Use nohup, disown or screen to leave a long running task in the background and get back to your shell.
Related
I have this command in my shell script that runs forever- it wouldn't finish unless I do ctrl-c. I have been trying to look up how to send ctrl-c signal to script and all the answers have been some sort of kill $! or kill$$ or such. My problem is that the command never finishes, so it never goes on to the next command like my "kill" commands or anything else. I have to manually hit the ctrl-C in my terminal for it to even execute kill $!. I'm sure there is a way to work around this but I am not sure what. Thanks in advance!
There are several approaches to this problem. The simplest (but not most robust) is (perhaps) to simply run your long running command in the background:
#!/bin/sh
long-running-command & # run in the background
sleep 5 # sleep for a bit
kill %1 # send SIGTERM to the command if it's still running
I am trying to launch a GUI application (rhythmbox) on a Ubuntu. In the following I try to explain the chain of executed files.
# Window manager executes first
~/i3wm_cmd_wrapper.sh Window_Name ~/mount_enc.sh
This wrapper uses gnome-terminal to execute stuff. This enables opening a terminal at startup where users can enter information.
# mount_enc.sh launches the following command in the end
bash ~/launch_in_bg.sh rhythmbox
mount_enc.sh does exactly what it is supposed to do when starting from a normal terminal. But I'd like to start it automatically at startup and rhythmbox should be kept open after the script is done.
# launch_in_bg.sh is just doing what it's supposed to
($PRGRM > /dev/null 2>&1) &
I can not get the gnome-terminal to open rhythmbox for me. Also I think my approach is wrong if I want rhythmbox to keep running after the gnome-terminal finishes executing the mount_enc.sh script. Can anybody think of a better solution?
If you open a program from the console (even in background), the process of the program is a child process of the console process and will terminate when the console process terminates.
To keep the program's process running it has to be detached from the console process. Detaching can be done in multiple ways. Some examples:
nohup rhythmbox &
or
rhythmbox & disown
To suppress output, use redirection as in your script.
I am making the "quit" command in a script that will act like "exit" but will close down the running bash terminal.
The only way I know how to do quit is:
exit
but that does no good - it just takes it as an exit for the script, not the bash terminal session.
You can try and kill the bash process by finding the process ID using top command and then kill #ID. This can end the bash session and keep the script running.
you can try executing that command like you were a shell proccess:
execp("exit",NULL,NULL);
Sounds like you are trying to detach a script from the shell you're in. If you want to detach from the shell from within the script itself you can effectively fork it ie.
#!bin/bash
forked_function() {
# This will now be in it's worn process. Check it using
# ps ax | grep forked_function
sleep 1000;
}
forked_function &
A very common approach is to use screen. This enables you to close the terminal and come back to the running process later where you left off. If all you want to do is run a script then exit try using an ampersand appended to the script you are trying to run ie
script &
exit
That will leave chrome running but allow you to detach it it it won't close when you close the terminal. You can also manage the tasks in the background. Have a look at man jobs
script &
jobs
You can use commands like fg 1 to bring job number 1 back to the foreground etc.
I'm trying to use a shell script to start a command. I don't care if/when/how/why it finishes. I want the process to start and run, but I want to be able to get back to my shell immediately...
You can just run the script in the background:
$ myscript &
Note that this is different from putting the & inside your script, which probably won't do what you want.
Everyone just forgot disown. So here is a summary:
& puts the job in the background.
Makes it block on attempting to read input, and
Makes the shell not wait for its completion.
disown removes the process from the shell's job control, but it still leaves it connected to the terminal.
One of the results is that the shell won't send it a SIGHUP(If the shell receives a SIGHUP, it also sends a SIGHUP to the process, which normally causes the process to terminate).
And obviously, it can only be applied to background jobs(because you cannot enter it when a foreground job is running).
nohup disconnects the process from the terminal, redirects its output to nohup.out and shields it from SIGHUP.
The process won't receive any sent SIGHUP.
Its completely independent from job control and could in principle be used also for foreground jobs(although that's not very useful).
Usually used with &(as a background job).
nohup cmd
doesn't hangup when you close the terminal. output by default goes to nohup.out
You can combine this with backgrounding,
nohup cmd &
and get rid of the output,
nohup cmd > /dev/null 2>&1 &
you can also disown a command. type cmd, Ctrl-Z, bg, disown
Alternatively, after you got the program running, you can hit Ctrl-Z which stops your program and then type
bg
which puts your last stopped program in the background. (Useful if your started something without '&' and still want it in the backgroung without restarting it)
screen -m -d $command$ starts the command in a detached session. You can use screen -r to attach to the started session. It is a wonderful tool, extremely useful also for remote sessions. Read more at man screen.
Put &| on the end of a command seems to detach the process from the shell. But where does it come from? and what's the right way to use it?
& will cause the process to be detached from the parent process (which in this case is the shell), but won't be disowned from it, which means when you'll close the shell, the process you started will be closed as well.
For disown it completely from the shell you need to do: my_process & and then disown %1
UPDATE
According to the information that the command &| ran on zsh the &| means:
&| - backgrounds the final command of the pipeline.
disown [ job ... ]
job ... &|
job ... &!
Remove the specified jobs from the job table; the shell will no longer report their status, and will not complain if you try to exit an interactive shell with them running or stopped. If no job is specified, disown the current job.
http://linux.die.net/man/1/zshbuiltins
Exit zsh, but leave running jobs open?
& will run the process in background. It will not occupy the terminal no more and wait it until finish.