I want to run a command silently via ssh and exit the shell, but the program should continue running.
I tried screen and nohup, but apparently with those it executes 3 processes instead of 1:
user:/bin/bash ./[script]
root: sudo [commandInTheScript]
root: [commandInTheScript]
What am I doing wrong?
P.S.: The thing is that I want to run this command with the Workflow app (iOS), but the app waits until the command is finished, so it freezes 'forever'
To run your process back ground, at end of the command you have to use &.
In your case, you have to run without session since you are planning to exit from ssh after execute the command, so you need nohup
nohup <command> &
nohup < command > &
This makes your command runs on background and shows its PID
How did you use nohup?
Eg.
nohup ruby server.rb &
Ampersand (&) is necessary to let command run in the background.
Related
I'm trying to execute the command:
ssh nvidia#ubuntu-ip-address "/opt/ads2/arm-linux64/bin/ads2 svcd&"
This works so far except that it hangs in the remote terminal when "/opt/ads2/arm-linux64/bin/ads2 svcd&" is executed, unless i enter ctrl+c. So I'm looking for a command that, after executing the command, exits from the remote terminal and continue executing the local bash script.
thanks in advance
When you run a command in background on a terminal, regardless of weather it be local or remotely, if you attempt to logout most systems will warn you have running jobs. One further attempt to logout and your jobs get killed as you exit.
In order to avoid this you need to detach your running jobs from terminal.
if job is already running you can
disown -h <jobspec ar reported by jobs>
If you want to run something in background and then exit leaving it running you can use nohup
nohup command &
This is certainly ok on init systems ... not sure if it works exactly like this on systems that use systemd.
I've tried nohup "sbt run" &
returns : nohup: failed to run command ‘sbt run’: No such file or directory
and tried :
nohup sbt run &
[2] 7897
# nohup: ignoring input and appending output to ‘nohup.out’
When I carriage return expecting process to continue running I receive :
[2]+ Stopped nohup sbt run
How to run sbt as a daemon ?
Update :
sbt run </dev/null &
[5] 8961
I think cd up one dir :
# cd ..
[5]+ Stopped sbt run < /dev/null (wd: /home/sum)
(wd now: /home)
So it starts as daemon but if I perform any actions such as changing dir it kills the process ? How to keep process running ?
Looks like sbt requested input from your terminal. If it does not really need input (which is probably the case when you run program in background), you can run it like this:
sbt run </dev/null >output-file &
See this answer for details.
EDIT
Ok, now that was a puzzle. Short answer: run sbt as follows:
setsid nohup sbt run &
Rationale:
The reason why sbt stops is arrival of SIGTTOU signal. It is delivered to background process in several cases, which include modifying terminal configuration. This is our case because according to strace -f sbt run &, sbt does a lot of black magic under the hood like this:
[pid 16600] execve("/usr/bin/sh", ["sh", "-c", "stty -g < /dev/tty"], [/* 75 vars */] <unfinished ...>
To work this around, you can run sbt in a different session to detach it from current terminal, so that it won't open /dev/tty and mess with our terminal.
This should also work
sbt -Djline.terminal=jline.UnsupportedTerminal run &
source: https://github.com/sbt/sbt/issues/701
oleg-andriyanov's answer did not work in my case.
(process exited soon after launch)
In a such case, try Mirko Stocker's command written in play ML below for alternative.
https://groups.google.com/forum/#!topic/play-framework/ZgjrPgib0-8
# screen -d -m sbt run
You can easily use tmux to do this (and persist anything else). A bonus feature is that if you install on a remote server you can persist jobs as "sessions" and reconnect to the same terminal "session". https://www.linode.com/docs/networking/ssh/persistent-terminal-sessions-with-tmux/
1) Launch your sbt job
sbt
run
2) detach with tmux session
ctrl+b (then release)
d
3) Show active tmux sessions (only occurs local tmux)
ctrl + b
s
4) Show all sessions on remote machine
$ tmux a
5) Attach session
$ tmux attach-session (your-session-number)
If you have already run sbt and you want to move it to the background then once the processes is in STOPPED condition, send it a CONTINUE signal with
kill -s SIGCONT PID
or
kill -s SIGCONT JOB_NUMBER
This will ensure that the sbt process is now running in the background.
I want to run jobs in the background so that I can logout from terminal once any job is started. I know two ways
1) run job and then press ctrl+z and then enter bg
mysqldump -uroot -p dbname > dbname.sql
2) using nohup to run job and then press ctrl+z and then enter bg
nohup mysqldump -u root -p dbname > dbname.sql 2>&1
I want to know the difference between above two commands and which one is best in which scenario.
Running your process as a job constrains your job to the current session. So if you run
$ start_running_job &
$ exit
The job will stop when you exit.
Pressing Control-Z has the same effect as the lines above.
Running nohup places the job so that it survives the end of the current session. So if you run
$ nohup start_running_job &
$ exit
The job will continue running.
I agree with answer provided...
"nohup" basically runs your command/script in back-end mode on server itself and it is not related to your current login session while putting "&" just puts the script in background and remains connected to your current login session. In any case you can use fg to bring back script/command.
Which is good for you, for this it depends how you are accessing your server and for how long your script is gonna take to complete. Say your script will take more amount of time and your current login session remains ideal it will close down and script will get killed if you have only given &. So much of the time just give nohup and don't worry it.
I see a lot of people are using the following command:
nohup ./sendData.sh >logs/send.log 2>&1 &
Since the nohup has already been used, do I still need to add & on the trail?
nohup tells the terminal to not hang up, even when the parent terminal is closed. & tells the terminal to send the process to the background. So
command &
will send the command to the background. You can then use the shell to invoke other commands. If the terminal is exited, the jobs are killed.
nohup command
will not send the command to the background, but when you close the terminal, the process will live on until it is finished or killed.
nohup command &
will send the command to the background and the process will not be killed even when you close the terminal. Usually when using nohup, one also wants the process to be in the background, that's why you see the combination of nohup and & more often than not.
I have written a shell script to execute a series of commands. One of the commands in the shell script is to launch an application. However, I do not know how to continue running the shell script after I have launched the application.
For example:
...
cp somedir/somefile .
./application
rm -rf somefile
Once I launched the application with "./application" I am no longer able to continue running the "rm -rf somefile" command, but I really need to remove the file from the directory.
Anyone have any ideas how to compete running the "rm -rf" command after launching the application?
Thanks
As pointed out by others, you can background the application (man bash 'job control', e.g.).
Also, you can use the wait builtin to explicitely await the background jobs later:
./application &
echo doing some more work
wait # wait for background jobs to complete
echo application has finished
You should really read the man pages and bash help for more details, as always:
http://unixhelp.ed.ac.uk/CGI/man-cgi?sh
http://www.gnu.org/s/bash/manual/bash.html#Job-Control-Builtins
Start the application in the background, this way the shell is not going to wait for it to terminate and will execute the consequent commands right after starting the application:
./application &
In the meantime, you can check the background jobs by using the jobs command and wait on them via wait and their ID. For example:
$ sleep 100 &
[1] 2098
$ jobs
[1]+ Running sleep 100 &
$ wait %1
put the started process to background:
./application &
You need to start the command in the background using '&' and maybe even nohup.
nohup ./application > log.out 2>&1