{"id":10844,"date":"2023-10-07T09:27:16","date_gmt":"2023-10-07T07:27:16","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=10844"},"modified":"2023-10-08T10:03:47","modified_gmt":"2023-10-08T08:03:47","slug":"how-to-use-the-bg-command-on-linux-pi-my-life-up","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2023\/10\/07\/how-to-use-the-bg-command-on-linux-pi-my-life-up\/","title":{"rendered":"How to use the bg Command on Linux &#8211; Pi My Life Up"},"content":{"rendered":"\n<p>Well, I learned a couple of details I didn&#8217;t knew from <em><a href=\"https:\/\/pimylifeup.com\/bg-command-linux\/\">How to use the bg Command on Linux &#8211; Pi My Life Up<\/a><\/em><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<!--more-->\n\n\n\n<!--nextpage-->\n\n\n\n<header class=\"entry-header post-header-outer row\">\n<h1 class=\"entry-title text-center col-12\">How to use the bg Command on Linux<\/h1>\n<div class=\"entry-meta header-meta-tags text-center col-12\">\n<div class=\"byline mx-2\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"avatar avatar-48 photo rounded\" src=\"https:\/\/i0.wp.com\/pimylifeup.com\/wp-content\/uploads\/2022\/04\/pimylifeup_editor_avatar.png?resize=48%2C48&#038;ssl=1\" alt=\"Emmet Avatar\" width=\"48\" height=\"48\"\/> by <span class=\"author vcard\"><span class=\"fn\">Emmet<\/span><\/span><\/div>\n<div class=\"posted-on mx-2 fa-calendar-alt\">Published <time class=\"entry-date date published\" datetime=\"2022-02-06T15:36:50+11:00\"> Feb 06, 2022<\/time><\/div>\n<div class=\"cat-links mx-2\"><a class=\"fa-linux\" href=\"https:\/\/pimylifeup.com\/category\/linux\/linux-commands\/\" rel=\"category tag\">Linux Commands<\/a><\/div>\n<div class=\"social-share-links mx-2 \">&nbsp;<\/div>\n<\/div>\n<\/header>\n\n\n\n<div class=\"entry-content\">\n<p class=\"has-text-align-center\">In this guide, we will be showing you how to use the bg command on a Linux-based system.<\/p>\n<p>The bg command allows you to run a job in the background by specifying its job ID. The most commonly used shells have support for this functionality.<\/p>\n<p>Alongside both <a href=\"https:\/\/pimylifeup.com\/fg-command-linux\/\">the fg command<\/a> and <a href=\"https:\/\/pimylifeup.com\/jobs-command-linux\/\">the jobs command<\/a>, this tool is a core part of the job control system. This system is what allows you to multitask within a shell session.<\/p>\n<p>Using this built-in utility, you can resume a suspended task within the current shell session. Suspending a process can be done easily by using <kbd>CTRL<\/kbd> + <kbd>Z<\/kbd> while running in the foreground.<\/p>\n<p>Over the next few sections, we will explore how you can use the bg command on your system.<\/p>\n<div class=\"slick-inline-search-panel\" data-config=\"postDCM_in-content\">&nbsp;<\/div>\n<h2 id=\"syntax-of-the-bg-command\" class=\"lbb-section-break mt-5 text-center wp-block-heading\">Syntax of the bg Command<\/h2>\n<p>The syntax for the bg command only requires you to know the ID of the job that you want to resume. However, there are other ways that you can use this command without knowing the ID.<\/p>\n<p>Below you can see the straightforward syntax of this function that is built-in to the shell.<\/p>\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">bg [JOBID]<\/code><\/pre>\n<p>Using the bg command without using the optional parameter will resume the current job. The current job is the last job sent to the background or suspended.<\/p>\n<p>The \u201c<code class=\"\" data-line=\"\">[JOBID]<\/code>\u201d parameter allows you to select the job you want to resume. There are various symbols that you can use here to select a job.<\/p>\n<ul>\n<li><code class=\"\" data-line=\"\">%JOBID<\/code> \u2013 If you know the ID of the job you want to resume, then all you need to use is the percentage sign, followed by the ID.\n<p>You can retrieve a list of the jobs and their ID by using the jobs command.<\/p>\n<p>For example, if we wanted to resume a suspended job with the ID of 4, we would use \u201c<code class=\"\" data-line=\"\">%4<\/code>\u201d as the job id.<\/p>\n<\/li>\n<li><code class=\"\" data-line=\"\">%STRING<\/code> \u2013 You can also select a job for the bg command by using a string. This string must match at least a partial part of the command.\n<p>For example, to match a ping command, you would use \u201c<code class=\"\" data-line=\"\">%ping<\/code>\u201d in place of the job ID.<\/p>\n<\/li>\n<li><code class=\"\" data-line=\"\">%+<\/code> or <code class=\"\" data-line=\"\">%%<\/code> \u2013 The bg command supports shorthand to select the current job. The current job is the one that you last placed into the background.\n<p>You need to use the percentage symbol (<code class=\"\" data-line=\"\">%<\/code>), followed by the plus symbol (<code class=\"\" data-line=\"\">+<\/code>). Alternatively, you can also use a double percentage sign (<code class=\"\" data-line=\"\">%%<\/code>) to achieve the same task.<\/p>\n<\/li>\n<li><code class=\"\" data-line=\"\">%-<\/code> \u2013 Selecting the previous job to resume is straightforward as well. All you need to do is use the percentage sign (<code class=\"\" data-line=\"\">%<\/code>) followed by the minus symbol (<code class=\"\" data-line=\"\">-<\/code>).\n<p>The previous job is the one used before the job that is now marked as the \u201c<strong>current<\/strong>\u201d job.<\/p>\n<\/li>\n<\/ul>\n<h2 id=\"examples-of-using-the-bg-command\" class=\"lbb-section-break mt-5 text-center wp-block-heading\">Examples of Using the bg Command<\/h2>\n<p>Let us run through some simple examples to give you an idea of how the bg command works.<\/p>\n<p>For this example, we suspended a few applications by using <kbd>CTRL<\/kbd> + <kbd>Z<\/kbd> in the terminal while they were running. You can also use the ampersand (<code class=\"\" data-line=\"\">&amp;<\/code>) at the end of a command to keep it running and place it into the background.<\/p>\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">pi@pimylifeup:~ $ jobs\n[1]-  Stopped                 ping google.com\n[2]   Running                 chromium-browser &amp;\n[3]   Stopped                 nano exampletext.txt\n[4]+  Stopped                 .\/examplescript.sh\n[5]   Stopped                 nano test\n<\/code><\/pre>\n<h3 id=\"resuming-a-job-by-using-its-id\" class=\"lbb-section-break mt-5 wp-block-heading\">Resuming a Job By Using its ID<\/h3>\n<p>The primary way that you will use the bg command is by referencing the job you want to resume by its ID.<\/p>\n<p>Thanks to the jobs command, you can easily get the ID for the job you want to resume. Additionally, when you suspend a job, its ID will be automatically printed to the command line.<\/p>\n<p>For this example, let us say that we wanted to resume our ping command by using the ID \u201c<code class=\"\" data-line=\"\">1<\/code>\u201c. This means all we need to do is use \u201c<code class=\"\" data-line=\"\">bg<\/code>\u201d followed by <code class=\"\" data-line=\"\">%1<\/code>\u201c.<\/p>\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">bg %1<\/code><a class=\"copy-code-block fa-copy\" href=\"https:\/\/pimylifeup.com\/bg-command-linux\/#copy\">Copy<\/a><\/pre>\n<h3 id=\"using-a-string-to-select-a-job-with-the-bg-command\" class=\"lbb-section-break mt-5 wp-block-heading\">Using a String to Select a Job with the bg Command<\/h3>\n<p>The bg command allows you to select a job by using a string. This string must be either part of or the entire command.<\/p>\n<p>For this example, if we wanted to resume our bash script, we would need to use the percentage sign (<code class=\"\" data-line=\"\">%<\/code>), followed by at least the start of the name.<\/p>\n<p>As shown below, we can get away with only including part of the command that started that job. In this case, we will use \u201c<code class=\"\" data-line=\"\">.\/example<\/code>\u201c.<\/p>\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">bg %.\/example<\/code><a class=\"copy-code-block fa-copy\" href=\"https:\/\/pimylifeup.com\/bg-command-linux\/#copy\">Copy<\/a><\/pre>\n<h3 id=\"selecting-the-current-job-using-the-bg-command\" class=\"lbb-section-break mt-5 wp-block-heading\">Selecting the Current Job using the bg Command<\/h3>\n<p>The utility allows us to use shorthand symbols to select the current job. These are useful if you only want to deal with the recently suspended command.<\/p>\n<p>The default behavior of the bg command is to utilize the current job. So you can simply just type in \u201c<code class=\"\" data-line=\"\">bg<\/code>\u201d into the terminal.<\/p>\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">bg<\/code><a class=\"copy-code-block fa-copy\" href=\"https:\/\/pimylifeup.com\/bg-command-linux\/#copy\">Copy<\/a><\/pre>\n<p>You can also use the percentage sign (<code class=\"\" data-line=\"\">%<\/code>) followed by the plus symbol (<code class=\"\" data-line=\"\">+<\/code>) to select the current job.<\/p>\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">bg %+<\/code><a class=\"copy-code-block fa-copy\" href=\"https:\/\/pimylifeup.com\/bg-command-linux\/#copy\">Copy<\/a><\/pre>\n<p>Alternatively, you can also use two percentage signs (<code class=\"\" data-line=\"\">%%<\/code>). This will also start the job in the background.<\/p>\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">bg %%<\/code><a class=\"copy-code-block fa-copy\" href=\"https:\/\/pimylifeup.com\/bg-command-linux\/#copy\">Copy<\/a><\/pre>\n<h3 id=\"resume-the-previous-job\" class=\"lbb-section-break mt-5 wp-block-heading\">Resume the Previous Job<\/h3>\n<p>The bg command also has additional functionality to select the \u201c<strong>previous<\/strong>\u201d job. This previous job is the one that you ran before the \u201c<strong>current<\/strong>\u201d job.<\/p>\n<p>You can start the previous job in the background using the bg command, followed by a percentage sign (<code class=\"\" data-line=\"\">%<\/code>) and a minus symbol (<code class=\"\" data-line=\"\">-<\/code>).<\/p>\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">bg %-<\/code><a class=\"copy-code-block fa-copy\" href=\"https:\/\/pimylifeup.com\/bg-command-linux\/#copy\">Copy<\/a><\/pre>\n<h2 id=\"sending-a-process-to-the-background\" class=\"lbb-section-break mt-5 text-center wp-block-heading\">Sending a Process to the Background<\/h2>\n<p>Whenever you typically run a command within a shell like bash it will automatically run in the foreground. However, there are a couple of ways to run this in the background instead.<\/p>\n<h3 id=\"using-the-ampersand\" class=\"lbb-section-break mt-5 wp-block-heading\">Using the Ampersand &amp;<\/h3>\n<p>When you run your command, including an ampersand (<code class=\"\" data-line=\"\">&amp;<\/code>) at the end tells the shell it should run it in the background.<\/p>\n<p>For example, if we use the following command, the ping command will continue to run in the background, but we will be able to continue to interact with the terminal.<\/p>\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">ping google.com &amp;<\/code><a class=\"copy-code-block fa-copy\" href=\"https:\/\/pimylifeup.com\/bg-command-linux\/#copy\">Copy<\/a><\/pre>\n<h3 id=\"suspending-the-current-process\" class=\"lbb-section-break mt-5 wp-block-heading\">Suspending the Current Process<\/h3>\n<p>You can also suspend the process you are currently running in the terminal. This process must be done while the process is still in the foreground.<\/p>\n<p>All you need to do to suspend a process within the shell is press <kbd>CTRL<\/kbd> + <kbd>Z<\/kbd> on your keyboard.<\/p>\n<h2 id=\"conclusion\" class=\"lbb-section-break mt-5 text-center wp-block-heading\">Conclusion<\/h2>\n<p>Hopefully, you now have an idea of how to use the bg command on your Linux system.<\/p>\n<p>This command is a crucial part of the job control system. Combined with other commands such as <code class=\"\" data-line=\"\">fg<\/code> and <code class=\"\" data-line=\"\">jobs<\/code>, you can multitask within your current shell session.<\/p>\n<p>If you have any questions about using the bg command, please comment below.<\/p>\n<p>You can also check out our other <a href=\"https:\/\/pimylifeup.com\/category\/linux\/linux-commands\/\">Linux command guides<\/a>, as well as our <a href=\"https:\/\/pimylifeup.com\/category\/linux\/\">Linux tutorials<\/a>.<\/p>\n<\/div>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Well, I learned a couple of details I didn&#8217;t knew from How to use the bg Command on Linux &#8211; Pi My Life Up<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2023\/10\/07\/how-to-use-the-bg-command-on-linux-pi-my-life-up\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-10844","post","type-post","status-publish","format-standard","hentry","category-senza-categoria"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-2OU","jetpack-related-posts":[{"id":11243,"url":"https:\/\/monodes.com\/predaelli\/2024\/01\/19\/pssh-run-commands-on-multiple-remote-linux-systems\/","url_meta":{"origin":10844,"position":0},"title":"PSSH &#8211; Run Commands on Multiple Remote Linux Systems","author":"Paolo Redaelli","date":"2024-01-19","format":"link","excerpt":"\u00a0PSSH is a small Python-based program, which allows you to execute commands on multiple Linux remote servers in parallel at the same time using the single shell. Source:- Run Commands on Multiple Remote Linux Systems","rel":"","context":"In &quot;Tricks&quot;","block_context":{"text":"Tricks","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/tricks\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1895,"url":"https:\/\/monodes.com\/predaelli\/2016\/11\/15\/50-most-frequently-used-unix-linux-commands-with-examples\/","url_meta":{"origin":10844,"position":1},"title":"50 Most Frequently Used UNIX \/ Linux Commands (With Examples)","author":"Paolo Redaelli","date":"2016-11-15","format":false,"excerpt":"Several pearl in 50 Most Frequently Used UNIX \/ Linux Commands (With Examples) Add line number for all non-empty-lines in a file $ sed '\/.\/=' thegeekstuff.txt | sed 'N; s\/\\n\/ \/' Remove duplicate lines using awk $ awk '!($0 in array) { array[$0]; print }' temp \u00a0 Print all lines\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9912,"url":"https:\/\/monodes.com\/predaelli\/2022\/12\/14\/how-to-configure-wireless-wake-on-lan-for-linux-wifi-card\/","url_meta":{"origin":10844,"position":2},"title":"How to configure wireless wake-on-lan for Linux WiFi card","author":"Paolo Redaelli","date":"2022-12-14","format":false,"excerpt":"How to configure wireless wake-on-lan for Linux WiFi card - nixCraft I have Network Attached Storage (NAS) server that backups all my devices. However, I am having a hard time with my Linux-powered laptop. I cannot back up my laptop\/computer when it is in suspended or sleep mode. How do\u2026","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11319,"url":"https:\/\/monodes.com\/predaelli\/2024\/02\/09\/how-to-record-and-replay-terminal-sessions-in-linux\/","url_meta":{"origin":10844,"position":3},"title":"How to Record and Replay Terminal Sessions in Linux","author":"Paolo Redaelli","date":"2024-02-09","format":false,"excerpt":"How to Record and Replay Terminal Sessions in Linux That is, just use script command...","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3717,"url":"https:\/\/monodes.com\/predaelli\/2018\/01\/28\/how-to-install-skype-for-linux-beta-on-debian-9-stretch-linuxbabe-com\/","url_meta":{"origin":10844,"position":4},"title":"How to Install Skype for Linux Beta on Debian 9 Stretch &#8211; LinuxBabe.Com","author":"Paolo Redaelli","date":"2018-01-28","format":false,"excerpt":"How to Install Skype for Linux Beta on Debian 9 Stretch - LinuxBabe.Com Skype for Linux beta 5.3 was released on May 31, 2017. It is a proprietary software, built on the web-based Skype client. This tutorial is going to show you how to install Skype on Debian 9 Stretch\u2026","rel":"","context":"In &quot;Debian&quot;","block_context":{"text":"Debian","link":"https:\/\/monodes.com\/predaelli\/category\/debian\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":315,"url":"https:\/\/monodes.com\/predaelli\/2015\/06\/12\/how-to-make-your-linux-pc-wake-from-sleep-automatically\/","url_meta":{"origin":10844,"position":5},"title":"How to Make Your Linux PC Wake From Sleep Automatically","author":"Paolo Redaelli","date":"2015-06-12","format":false,"excerpt":"From How to Make Your Linux PC Wake From Sleep Automatically I use sudo rtcwake -m\u00a0mem -l -t $(date +%s -d \u2018tomorrow 06:30\u2019) or sudo \/usr\/sbin\/rtcwake -m mem -s $(( 8 * 3600)) Want to put your Linux PC into sleep or hibernate mode and have it automatically wake at\u2026","rel":"","context":"In &quot;GNU\/Linux&quot;","block_context":{"text":"GNU\/Linux","link":"https:\/\/monodes.com\/predaelli\/category\/gnulinux\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2015\/06\/image101.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2015\/06\/image101.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2015\/06\/image101.png?resize=525%2C300 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/10844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=10844"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/10844\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=10844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=10844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=10844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}