16

I use the command $ nohup ... to start an application in the background so that I can close the terminal window afterwards.

Upon execution it creates the file ~/nohup.out.

Example:

orschiro@x230:~$ nohup /bin/bash -c "sleep 15 && python3 /home/orschiro/bin/show_state.py"
nohup: ignoring input and appending output to 'nohup.out'

How can I prevent nohup from creating nohup.out?

orschiro
  • 13,737

1 Answers1

22

You may redirect the output anywhere you like, for example:

nohup /bin/bash -c "sleep 15 && python3 /home/orschiro/bin/show_state.py" >/dev/null 2>&1

redirection prevents the default output file nohup.out from being created. /dev/null is oblivion, so if you don't want the data at all, send it there.

In bash you can abbreviate >/dev/null 2>&1 to &>/dev/null

For programs I use often I make an alias (and add it to my .bashrc) for example

alias show_state='nohup /bin/bash -c "sleep 15 && python3 /home/orschiro/bin/show_state.py" >/dev/null 2>&1'

Now all I have to type is show_state

Zanna
  • 72,471
  • 2
    @orschiro: After a few years it won't be cryptic and you'll remember it ;) I suggest sticking with it, because aliasing it away means you won't gain that experience, and knowing how BASH redirection operators work off the top of your head is a crucial skill. – Lightness Races in Orbit Aug 31 '16 at 08:49
  • Wouldn't this be better off as a function? Something like show_state () { /bin/bash -c "$@" &>/dev/null }? (Warning: poor syntax and probably won't work! It's just an example!). One nitpick: why show_state when you're throwing away the output of nohup? – Ismael Miguel Aug 31 '16 at 10:31
  • I don't know what the script does @IsmaelMiguel but I assume it is an indicator with a tray icon, not to print "state" (whatever it is) to stdout. Sure, a function may well be more fun than an alias ^.^ – Zanna Aug 31 '16 at 10:48
  • Oh, I got it! Sorry, didn't see the name of the .py file. And yes, a function is more fun. I have this badly written function on a Debian desktop: shutup() { source "$@" >/dev/null 2>&1 & }. And it works. Maybe you could suggest something similar, but a lot better? – Ismael Miguel Aug 31 '16 at 11:43
  • I never knew that &> worked (and to my mind it looks like background plus stdout redirection). I always learned it as >&. Is it standard? – Random832 Aug 31 '16 at 14:04
  • @IsmaelMiguel it's your idea, why don't you add an answer ;) – Zanna Aug 31 '16 at 15:05
  • @Random832 &>outfile is a bash-ism, >outfile 2>&1 is standard – Zanna Aug 31 '16 at 15:07
  • How does redirection work if a nohup.out file is created instead of standard output? – EKons Aug 31 '16 at 15:20
  • @ΈρικΚωνσταντόπουλος nifty feature of nohup... read the source code and get back to us :) – Zanna Aug 31 '16 at 15:25
  • @Zanna Where is the source code exactly? I can only find a binary, a man page, and a nohup.c (note: this is for OS X as far as I can tell) without the FILE datatype. Strange... – EKons Aug 31 '16 at 15:31
  • @orschiro you need to remember it this way: first redirect standard output (>) to /dev/null, then redirect standard error (2>) to standard output (&1). – Andrea Lazzarotto Aug 31 '16 at 15:50
  • @ΈρικΚωνσταντόπουλος nohup.c is the source code (in coreutils package source) – Zanna Aug 31 '16 at 15:58
  • @Zanna I am talking about &> outfile vs >& outfile. I can't find a description of >& outfile, but io_file : GREATAND filename does appear in the official grammar - anyway, it's supported more widely on actual shells other than bash, so it's probably better to learn it than &> in any case. – Random832 Aug 31 '16 at 18:43
  • On reviewing the standard further I was able to determine that the behavior is unspecified. It does give a more informative error than &> in dash (which does indeed interpret the latter as "background, then redirect [for a second, null, command]") though. – Random832 Aug 31 '16 at 18:56
  • @ΈρικΚωνσταντόπουλος The behavior of only opening nohup.out if the existing standard output is a terminal is standard, see this page, so you can rely on it on most systems including OSX. It will also automatically redirect stderr to stdout if you only redirected stdout, so you can just do nohup ... > /dev/null. – Random832 Aug 31 '16 at 19:00
  • Because your answer was already upvoted and accepted. Adding an answer showing an alternative would be pretty much useless. Nobody would even care about it. That's why I don't post an answer. – Ismael Miguel Aug 31 '16 at 19:10