Run Process in Background Linux Terminal

Updated on May 11, 2016

If you’re like me and you started to hate clicking icons to launch projects, you start them from the shell. For example, I open my terminal or have an open terminal and run the vlc command. This command will open the VLC mediaplayer if you have it installed. But now the process is running in the foreground on your open terminal. Vlc’s lifecycle is closely tied to the terminal and blocks other actions on the terminal. If you don’t have vlc use the ‘gnome-calculator’ for example.

When you start the process this way, you’ll see the shell waits (blocks) until the program finishes or is terminated.

If you close the terminal you will be prompted by the terminal: “There is still a process running in this terminal. Closing the terminal will kill it”. You can cancel or close the terminal. This is because the process your started inherits some things from the shell (stdin, stdout, etc) . This makes that the process is bound to that terminal.

When you close your terminal, the shell receives a SIGHUP, it also sends a SIGHUP to the process you started, this will end the process.

Let’s assume you don’t want to kill the process, but you don’t need or want this terminal anymore. Or you want to continue using the terminal for other tasks.

Adding the ‘&’ to your command

In the case of vlc & the process moves to the background and you can continue to use the terminal as you see fit. You are You get a shell prompt again. Often I just continue from there or clear the terminal and use it for other tasks or starting different processes.

If you close the terminal by pressing the close button (x) with the mouse or Shift+Ctrl+W, then it will stop all the processes you started in that terminal. This is more or less the same problem as before, but this time the terminal doesn’t prompt you to do so, it just kills the program(s) immediately.

If you type the exit command in the terminal or close with the Ctrl+D shortcut it will close the terminal, but the running processes will stay up. This was the first solution I tried. I still use it quite often, but there are others. Some have other benefits or give you more options if needed.

$ vlc &

Adding the ‘& disown’ to your command

In many ways this (& disown) seems to do the same as adding the “&” to the command. But when you add disown, the process will be removed from the shell’s list of jobs. The process is still connected to the terminal, but it does not receive the SIGHUP.

One of the downsides of disown is that it is bash specific. If you often use different systems which don’t know this command then it can be a pain. Since most of the commands are near reflex like after a while, you could end up typing this command on systems where it is of no use. But for me this is not really an issue, but keep it in mind.

$ vlc & disown

Using ‘setsid’ in your shell command

A command run with setsid opens and/or assigns a new session id to the process. This session id is different from the session id of the current running terminal. A tree of all processes (executing instances of the programs) running on your system, would show that the process with a new session id starts at the root of the tree structure, not as a branch of the terminal process.

With setsid you can’t check the output of the process you started later on. This is something that you can do with for example nohup and screen. With screen there is an attached screen session to the process and with nohup you can check the output file. But is this something you need or want for the process you are starting? Sometimes this is the case, but often you just need to run the program and that’s it.

You can also make it write to an output file when using setsid.

$ setsid vlc

Using ‘nohup’ with your command

Nohup will seperate the process from the terminal. Because of the disconnect it will not receive SIGHUP, which is why it is named nohup. If you close the terminal the process will keep running until you close it.

When the terminal is open, the job is still in the foreground and still under the shell’s job control. This means if you just use nohup that you still can’t use that terminal for other tasks. So you should combine it with an ‘&’.

$ nohup vlc
$ nohup vlc &

As you see it closes/ignores the input and will append the output to a nohup.out. The use of nohup.out will make sure the process can still write its output even when the terminal ends/closes.

The Run Dialog

You can also opt for not starting the process from a terminal. It is possible to use the run dialog prompt. Press ALT+F2 and type in the process you wish to start in the dialog screen. For starting VLC mediaplayer in our case example: vlc.

Linux basic commands, ubuntu

Questions & Answers

    Comments

      0 of 8192 characters used
      Post Comment

      No comments yet.

      working

      This website uses cookies

      As a user in the EEA, your approval is needed on a few things. To provide a better website experience, turbofuture.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

      For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://turbofuture.com/privacy-policy#gdpr

      Show Details
      Necessary
      HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
      LoginThis is necessary to sign in to the HubPages Service.
      Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
      AkismetThis is used to detect comment spam. (Privacy Policy)
      HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
      HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
      Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
      CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
      Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
      Features
      Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
      Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
      Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
      Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
      Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
      VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
      PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
      Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
      MavenThis supports the Maven widget and search functionality. (Privacy Policy)
      Marketing
      Google AdSenseThis is an ad network. (Privacy Policy)
      Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
      Index ExchangeThis is an ad network. (Privacy Policy)
      SovrnThis is an ad network. (Privacy Policy)
      Facebook AdsThis is an ad network. (Privacy Policy)
      Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
      AppNexusThis is an ad network. (Privacy Policy)
      OpenxThis is an ad network. (Privacy Policy)
      Rubicon ProjectThis is an ad network. (Privacy Policy)
      TripleLiftThis is an ad network. (Privacy Policy)
      Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
      Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
      Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
      Statistics
      Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
      ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
      Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)