Running a command in the background on macOS

by on Mar.01, 2024, under News

To run a command in the background on macOS, so that it continues running even after closing the Terminal window, we can use the nohup command. The nohup command (short for “no hang up”) allows a process to continue running in the background after you have logged out from a shell.

Here’s how we can use it:

nohup my_command &

This will start my_command in the background. After executing this command, we’ll see a message indicating that the output will be written to nohup.out (unless otherwise specified).

To run a command in the background on macOS without creating a nohup.out file, we can redirect the output to /dev/null. This effectively discards all output (both standard output and error output) from the command. Here’s how we can do it:

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

In this example we pipe the output (of nohup) to /dev/null which effectively discards it.

2>&1 redirects the standard error (stderr) to the standard output, which has already been redirected to /dev/null.

We can now safely close the Terminal window and our command will continue to run in the background, while no output file is being created. This method is useful when we’re not interested in the output of the command or when we’re certain that it won’t produce any critical output that needs monitoring.

Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!