Linux Commands Examples

A great documentation place for Linux commands

nohup

run a command immune to hangups, with output to a non-tty

Synopsis

nohup COMMAND [ARG]...
nohup
OPTION


add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!

examples

2
source
            
nohup ./nohup.sh
0
source
            
nohup ./run &
tail -f nohup.out
0
source
            
nohup rails s
zsh
0
source
            
nohup ./testreadblock $1 &
0
source
            
nohup ./go.sh run&
0
source
            
nohup ./ehcpdaemon.sh &
0
source
            
nohup ./main.sh &
0
source

How do I fork a process that doesn't die when shell exits?

You do not mention if this is running as an X app or a console app.

If it's as a console app, of course it needs to close. You got rid of it's input/output, more technically the (pseudo) tty it was on. It's very unlikely this is what you meant, so let's assume you're talking about an X app.

nohup should work, not sure why it isn't. When the shell closes, it sends SIGHUP to all processes in its process group. nohup tells the command to ignore SIGHUP.

You can also try setsid, which disconnects the process from the process group

alias emacs='setsid emacs'

Or add disown after &

0
source

How to attach to a nohup process?

You should have used screen in the first place as it is a lot more flexible than nohup.

But if you want to attach to the process in the same way as screen, this link provides more info : http://serverfault.com/questions/24425/can-i-nohup-screen-an-already-started-process

Basically you can either use reptyr as described in the link, or you can use an old script called screenify. I found the script here : http://isteve.bofh.cz/~isteve/knowledgebase/articles/screenify.html

0
source

Can I force nohup output to the screen instead of a file?

That would defeat the purpose of nohup, use tail -f out.log instead.

0
source

Is it possible to change a running process in linux, so that it doesn't quit upon logging out?

In Bash, you could disown the process.

disown [-ar] [-h] [jobspec … ]     

Each jobspec is removed from the table of active jobs.

  • -h SIGHUP is not sent to the job if the shell receives a SIGHUP. current job .
  • -a all the -r running jobs.

See also: Shell stuff: job control and screen

If you use the bash shell, then you have an alternative (don't you always?) Instead of using nohup, just run the command normally, put it in the background one of the two ways we've discussed, and then disown -h the job.

$ tar cjf /backup/rob/home.tar.bz2 . &

[1] 32089
$ disown -h

You can then safely logout or close your terminal. As with nohup, if you close the terminal or logout, you will not be able to access that command directly using the jobs and %<N> or fg <N> commands..

Related posts on the Stack Exchange network:

0
source

Continue SSH background task/jobs when closing SSH

When you use screen you need to detach with CTRL+A+D before you exit ssh.

Alternatively, if you want to run a process directly with screen you can use

screen -dmSL [session name] [commands]
  • -d starts a screen session and immediately detaches from it
  • -m forces creating a new screen session
  • -S lets you give the session a name
  • -L turns on logging to ~/screenlog.0

example:

screen -dmSL workstuff myscript.sh

You can then either:

resume later using screen -x workstuff

or

check the log file less -r ~/screenlog.0

0
source

Correct Method of Utilizing nohup in Batch Process

Putting nohup on each line is not the answer.  If you do that, and the session is terminated, the currently-running program will run to completion, but then the script will terminate without finishing.  nohup filebuilder.sh will work, but then you have to remember to do it every time.  A couple of solutions:

  1. Write filebuilder.sh to say

    exec nohup name_of_the_real_filebuilder.sh

  2. Put

    trap "" 1

at the beginning of your script.  (1 is the numeric value of the “hangup” signal.)

0
source

Nohup does not run process in background

nohup bash -c "while [ true ]; do echo test; done" &

Nohup provides you immunity to hangup signals. But it does not automatically set the process to background.

It's the & at the end of the command that causes it to run in the background.

0
source

How to resume simulations running in background

If jobs were interrupted due to insufficient disk space, they most likely exited completely. This depends on the way they were programmed, but I can't imagine any process waiting for user input before exiting due to failed write operations or other symptoms of a full disk.

There's no way to "resume" other than starting the processes again.

The only possibility you have is when the process (whatever it is) is aware of what output it has already produced and where it needs to start again. For example, the process could scan its output for already existing items and then continue where it left off. But no process does this by default, you need to explicitly program it this way.

It all depends on what exactly this process is, what it takes as input, and what output it produces (so, if we knew that, we could probably help you better).

0
source

Using nohup in Ubuntu - how to make it not show the job number?

The [1] 27918 is coming from the & not the nohup.

Put the command you want to run in the background inside a script file:

#!/bin/sh
/path/to/command & >/dev/null 2>/dev/null

and then call that with nohup

nohup sh /path/to/my/script > foo.out 2> foo.err < /dev/null

The output of & is then redirected to /dev/null inside the script.

0
source

Background process (nohup &) suspends/resumes when user logs out/logs in

I usually redirect stdout and stderr as well as using nohup. I'm not sure if it is necessary but it works for me.

If the program ever reads from stdin it will wait.

0
source

can I `dtach` or `renice` graphical programs like `evince`?

dtach does not influence OS resources in the sense that it reduces RAM or CPU cycles, dtach detaches a process from it's parent process. renice on the other hand increases / decreases the priority of the process for the schedular; the process will gain more cpu-cycles .

So: yes, you can use dtach to detach evince from your xterm (I doubt that you open evince via xterm anyway). This would only ensure that closing xterm won't close evince. Yes, you can renice a lower priority to evince and then the scheduler will call evince less often. Memory wise there won't be any change at all. To reduce work load you might minimize evince so it is not visible and thus nothing new will be rendered and no checks against overlapping due to other programms will take place.

But, and I mean that in all seriousness: Stop fiddling around with your system in such micromanagement style and just buy more RAM. As long as you don't open 1000s of evince to be read later (which is a usage pattern I would change in the first place) the OS will behave not really differently when you microtune the OS. If you don't want to read the .pdfs now: save them to disk. Problem solved.

0
source

How to find out is a program run with nohup or not?

  • You need to know pid of process you want to look at. You can use pgrep or jobs -l:

    jobs -l
    [1]-  3730 Running    sleep 1000 &
    [2]+  3734 Running    nohup sleep 1000 &
    

    Actually, this might already tell you the answer, unless you logged off and logged in again (then jobs -l will not show anything).

  • Take a look at /proc/<pid>/fd. Processes started with nohup will list nohup.out files:

    mvp@linux:~$ ls -l /proc/3734/fd
    total 0
    l-wx------ 1 mvp mvp 64 Oct 29 02:32 0 -> /dev/null
    l-wx------ 1 mvp mvp 64 Oct 29 02:32 1 -> /home/mvp/nohup.out
    l-wx------ 1 mvp mvp 64 Oct 29 02:32 2 -> /home/mvp/nohup.out
    

    and started without nohup will not:

    mvp@linux:~$ ls -l /proc/3730/fd
    total 0
    lrwx------ 1 mvp mvp 64 Oct 29 02:28 0 -> /dev/pts/1
    lrwx------ 1 mvp mvp 64 Oct 29 02:28 1 -> /dev/pts/1
    lrwx------ 1 mvp mvp 64 Oct 29 02:28 2 -> /dev/pts/1
    
0
source

Ctrl+c in a sub process is killing a nohup'ed process earlier in the script

Have you tried disown?

  disown -h %1

or whatever your job is; disown is a shell built-in, its man page states:

disown

disown [-ar] [-h] [jobspec ...]

Without options, each jobspec is removed from the table of active jobs. If the -h' option is given, the job is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If jobspec is not present, and neither the-a' nor -r' option is supplied, the current job is used. If no jobspec is supplied, the-a' option means to remove or mark all jobs; the `-r' option without a jobspec argument restricts operation to running jobs.

EDIT

Funny thing, your construction works on my Arch Linux:

 $ cat testm
  #!/bin/sh

  nohup /home/mario/temp/waste & 

  less +F out.log

 $ cat waste
  #!/bin/sh

  while [ 1 ]; do
    find / -print 2>1 1> out.log
  done
  $ ./testm
   nohup: appending output to ‘nohup.out’
  $ ps ax | grep waste
    19090 pts/2    S      0:00 /bin/sh /home/mario/temp/waste
    19124 pts/2    S+     0:00 grep waste]
  $

Before the ps command, I had to scroll the out.log file, then Ctrl+C, then q.

description

Run COMMAND, ignoring hangup signals.

--help

display this help and exit

--version

output version information and exit

If standard input is a terminal, redirect it from /dev/null. If standard output is a terminal, append output to ’nohup.out’ if possible, ’$HOME/nohup.out’ otherwise. If standard error is a terminal, redirect it to standard output. To save output to FILE, use ’nohup COMMAND > FILE’.

NOTE: your shell may have its own version of nohup, which usually supersedes the version described here. Please refer to your shell’s documentation for details about the options it supports.

copyright

Copyright © 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

reporting bugs

Report nohup bugs to bug-coreutils[:at:]gnu[:dot:]org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report nohup translation bugs to <http://translationproject.org/team/>


see also

The full documentation for nohup is maintained as a Texinfo manual. If the info and nohup programs are properly installed at your site, the command

info coreutils 'nohup invocation'

should give you access to the complete manual.


author

Written by Jim Meyering.

How can this site be more helpful to YOU ?


give  feedback