Schmidt Nest πŸš€

How do I use the nohup command without getting nohupout

April 4, 2025

πŸ“‚ Categories: Programming
🏷 Tags: Unix Jobs
How do I use the nohup command without getting nohupout

Moving agelong-moving processes connected a distant server is a communal project for scheme directors and builders. The nohup bid is a lifesaver successful these situations, permitting instructions to proceed moving equal last you log retired. Nevertheless, the default behaviour of nohup is to redirect output to a record named nohup.retired, which tin rapidly go unwieldy and hard to negociate. This article explores however to usage the nohup bid much efficaciously by controlling its output and integrating it seamlessly into your workflow. We’ll screen redirecting output to customized information, discarding output altogether, and champion practices for managing agelong-moving processes.

Redirecting Output to a Circumstantial Record

The easiest manner to debar the default nohup.retired record is to redirect the output to a record of your selecting. This is achieved utilizing modular output redirection. For illustration, to redirect output to a record named my_output.log, usage the pursuing bid:

nohup my_command > my_output.log &

This bid directs modular output (stdout) to my_output.log. If your bid besides generates mistake messages, you mightiness privation to redirect these arsenic fine:

nohup my_command > my_output.log 2>&1 &

This redirects some modular output (stdout, represented by 1) and modular mistake (stderr, represented by 2) to the aforesaid record.

Discarding Output Wholly

Successful any instances, you mightiness not demand to prevention the output astatine each. You tin discard the output by redirecting it to /dev/null:

nohup my_command > /dev/null 2>&1 &

This efficaciously silences the bid, stopping immoderate output from being saved oregon displayed.

Utilizing Logging with nohup

For analyzable scripts oregon functions, utilizing a devoted logging mechanics supplies much power and flexibility than elemental redirection. See integrating a logging room inside your book, specified arsenic Python’s logging module oregon akin instruments successful another languages. This permits you to configure log ranges, formatting, and antithetic output locations.

For case, inside a Python book, you tin configure logging to compose to a circumstantial record:

import logging logging.basicConfig(filename='my_script.log', flat=logging.Information) logging.information("Beginning my book...") ... remainder of your book ... 

This attack supplies much granular power complete your logs and avoids the limitations of basal redirection.

Champion Practices for Managing nohup Processes

Managing aggregate nohup processes requires cautious formation. See these champion practices:

  • Usage descriptive filenames for output logs.
  • Instrumentality logging inside your scripts for amended power and formation.

Usage the jobs bid to database moving inheritance processes. You tin past usage fg to deliver a inheritance procedure to the foreground oregon termination %job_number to terminate a circumstantial procedure.

  1. Tally jobs to database inheritance jobs.
  2. Place the occupation figure of your nohup procedure.
  3. Usage termination %job_number to terminate the procedure.

Monitoring processes and their related output information is important. See utilizing a procedure director similar tmux oregon surface for amended conference direction and power complete your agelong-moving processes. This tin simplify managing aggregate processes and forestall them from being terminated unexpectedly.

For much elaborate accusation connected procedure direction, mention to the Bash guide connected occupation power.

Different adjuvant assets for knowing indicators is the impressive(7) male leaf.

Illustration: Moving a Python Book with nohup and Logging

Fto’s opportunity you person a Python book named my_script.py. To tally it successful the inheritance with nohup and log output to a record, usage the pursuing bid:

nohup python my_script.py > my_script.log 2>&1 & 

Wrong my_script.py, usage the logging module arsenic demonstrated earlier to compose messages to the log record.

[Infographic depicting however output redirection plant with nohup]

Leveraging the nohup bid efficaciously tin importantly streamline your workflow once dealing with agelong-moving processes. By knowing output redirection, logging strategies, and champion practices for managing inheritance jobs, you tin guarantee that your scripts tally easily and that you person the essential accusation astatine your fingertips. Retrieve to take the methodology that champion fits your wants, whether or not it’s redirecting to a circumstantial record, discarding output, oregon using a strong logging mechanics. Research much precocious methods for procedure direction to additional heighten your power complete agelong-moving duties.

By implementing these methods, you tin efficaciously negociate agelong-moving processes with out the muddle of nohup.retired and keep a cleanable and organized server situation. Larn much astir Linux procedure direction and research precocious instruments similar tmux and surface to heighten your server medication expertise. The nohup documentation besides offers invaluable accusation. Present, return power of your nohup output and optimize your workflow!

FAQ

Q: What if I privation to append output to an present record alternatively of overwriting it?

A: Usage the >> function alternatively of >. For illustration: nohup my_command >> my_output.log 2>&1 &

Question & Answer :
I person a job with the nohup bid.

Once I tally my occupation, I person a batch of information. The output nohup.retired turns into excessively ample and my procedure slows behind. However tin I tally this bid with out getting nohup.retired?

The nohup bid lone writes to nohup.retired if the output would other spell to the terminal. If you person redirected the output of the bid location other - together with /dev/null - that’s wherever it goes alternatively.

nohup bid >/dev/null 2>&1 # doesn't make nohup.retired 

Line that the >/dev/null 2>&1 series tin beryllium abbreviated to conscionable >&/dev/null successful about (however not each) shells.

If you’re utilizing nohup, that most likely means you privation to tally the bid successful the inheritance by placing different & connected the extremity of the entire happening:

nohup bid >/dev/null 2>&1 & # runs successful inheritance, inactive doesn't make nohup.retired 

Connected Linux, moving a occupation with nohup routinely closes its enter arsenic fine. Connected another methods, notably BSD and macOS, that is not the lawsuit, truthful once moving successful the inheritance, you mightiness privation to adjacent enter manually. Piece closing enter has nary consequence connected the instauration oregon not of nohup.retired, it avoids different job: if a inheritance procedure tries to publication thing from a inactive-unfastened modular enter, it volition intermission, ready for you to deliver it backmost to the foreground and kind thing. Truthful the other-harmless interpretation appears similar this:

nohup bid </dev/null >/dev/null 2>&1 & # wholly indifferent from terminal 

Line, nevertheless, that this does not forestall the bid from accessing the terminal straight, nor does it distance it from your ammunition’s procedure radical. If you privation to bash the second, and you are moving bash, ksh, oregon zsh, you tin bash truthful by moving disown with nary statement arsenic the adjacent bid. That volition average the inheritance procedure is nary longer related with a ammunition “occupation” and volition not person immoderate indicators forwarded to it from the ammunition. (A disowned procedure will get nary indicators forwarded to it robotically by its genitor ammunition - however with out nohup, it volition inactive have a HUP impressive dispatched by way of another means, specified arsenic a guide termination bid. A nohup‘ed procedure ignores immoderate and each HUP indicators, nary substance however they are dispatched.)

Mentation:

Successful Unixy programs, all origin of enter oregon mark of output has a figure related with it known as a “record descriptor”, oregon “fd” for abbreviated. All moving programme (“procedure”) has its ain fit of these, and once a fresh procedure begins ahead it has 3 of them already unfastened: “modular enter”, which is fd zero, is unfastened for the procedure to publication from, piece “modular output” (fd 1) and “modular mistake” (fd 2) are unfastened for it to compose to. If you conscionable tally a bid successful a terminal framework, past by default, thing you kind goes to its modular enter, piece some its modular output and modular mistake acquire dispatched to that framework.

However you tin inquire the ammunition to alteration wherever immoderate oregon each of these record descriptors component earlier launching the bid; that’s what the redirection (<, <<, >, >>) and tube (|) operators bash.

The tube is the easiest of these… command1 | command2 arranges for the modular output of command1 to provender straight into the modular enter of command2. This is a precise useful agreement that has led to a peculiar plan form successful UNIX instruments (and explains the beingness of modular mistake, which permits a programme to direct messages to the person equal although its output is going into the adjacent programme successful the pipeline). However you tin lone tube modular output to modular enter; you tin’t direct immoderate another record descriptors to a tube with out any juggling.

The redirection operators are friendlier successful that they fto you specify which record descriptor to redirect. Truthful zero<infile reads modular enter from the record named infile, piece 2>>logfile appends modular mistake to the extremity of the record named logfile. If you don’t specify a figure, past enter redirection defaults to fd zero (< is the aforesaid arsenic zero<), piece output redirection defaults to fd 1 (> is the aforesaid arsenic 1>).

Besides, you tin harvester record descriptors unneurotic: 2>&1 means “direct modular mistake wherever modular output is going”. That means that you acquire a azygous watercourse of output that contains some modular retired and modular mistake intermixed with nary manner to abstracted them anymore, however it besides means that you tin see modular mistake successful a tube.

Truthful the series >/dev/null 2>&1 means “direct modular output to /dev/null” (which is a particular instrumentality that conscionable throws distant any you compose to it) “and past direct modular mistake to wherever modular output is going” (which we conscionable made certain was /dev/null). Fundamentally, “propulsion distant any this bid writes to both record descriptor”.

Once nohup detects that neither its modular mistake nor output is hooked up to a terminal, it doesn’t fuss to make nohup.retired, however assumes that the output is already redirected wherever the person desires it to spell.

The /dev/null instrumentality plant for enter, excessively; if you tally a bid with </dev/null, past immoderate effort by that bid to publication from modular enter volition immediately brush extremity-of-record. Line that the merge syntax gained’t person the aforesaid consequence present; it lone plant to component a record descriptor to different 1 that’s unfastened successful the aforesaid absorption (enter oregon output). The ammunition volition fto you bash >/dev/null <&1, however that winds ahead creating a procedure with an enter record descriptor unfastened connected an output watercourse, truthful alternatively of conscionable hitting extremity-of-record, immoderate publication effort volition set off a deadly “invalid record descriptor” mistake.

To reply Gwangmu Lee’s motion: location’s nary magic successful nohup, since it’s not equal a ammunition builtin (astatine slightest not successful bash and zsh; it is successful ksh, which permits you to usage it to tally another ammunition builtins). Immoderate bid tin cheque to seat if an unfastened record descriptor factors to the terminal oregon not, which is each it does to find whether or not oregon not to compose the nohup.retired record.

Location aren’t immoderate builtin ammunition instructions that bash redirection for you; that’s ever nether your specific power. I fishy that’s due to the fact that it would past beryllium complicated if you added specific redirections to a bid that had implicit ones. However location’s thing stopping you from doing redirects successful your ain scripts and features. For illustration:

run_detached() { nohup "$@" </dev/null >/dev/null 2>&1 & disown }