Linux run in place

Create screen and run command without attaching

I am working on automating a maintenance routine that involves starting and stopping a script that is running in a screen session. My approach is to kill the screen session, and then restart it and run the command from within a script using the abilities to both create a screen and pass a command without needing to attach to the screen. However, I am having difficulties with this. I can create the screen correctly without it attaching using screen -d -m -S screen_name . However, if I run a command based on: screen -S screen_name-X stuff «command 1″‘echo -ne ‘\015»»command 2″‘echo -ne ‘\015» with the echo -ne ‘\015’ being wrapped with backticks rather than single quotes. It is to simulate the user pressing the enter key as the commands I use are moving to a directory and executing a script located there. This command works, but only if the screen has been attached to once it has been created. As I am trying to automate the process of creating the screen and running the commands within it I would like to avoid having to attach and detach within a script. I will be trying the suggestion of creating a shell script containing the commands I need to execute within the screen and edit according to my results. Is there a way to create a screen and run a command within the screen either in one command, or without having to attach to the screen after creating but before execution of the command? Thanks in advance. **Update — having tried the suggestion to place the commands I need to execute within a shell script I have been able to successfully create a screen and execute the commands from within the screen, but I am getting the behaviour that when the script stops running the screen closes as well. This shouldnt be a problem as the script is a logging script that should only stop with the knowledge of the sys admin or through the script I am trying to develop, however it would be preferable to have the screen setup in such a way that the screen does not disappear if the script is stopped. Is it possible to achieve this behaviour? **

I ran into this same problem and found a solution on superuser.com for anyone else who stumbles upon this problem superuser.com/questions/342463/…

For your auto-closing issue, you could use a special .screenrc that contains the line zombie kr , which will keep a finished window open, and you can press k to close the winodw, or r to run the command in the window again. I have this for my default .screenrc.

Источник

How to Run Linux Commands in Background and Detach in Terminal

In this guide, we shall bring to light a simple yet important concept in process handling in a Linux system, which is how to completely detach a process from its controlling terminal.

Читайте также:  Mount sftp as folder linux

When a Linux process is associated with a terminal, two problems might occur:

  1. Your controlling terminal is filled with so much output data and error/diagnostic messages.
  2. In the event that the terminal is closed, the process together with its child processes will be terminated.

To deal with these two issues, you need to totally detach a process from a controlling terminal. Before we actually move to solve the problem, let us briefly cover how to run processes in the background in Linux.

Run Linux Command or Process in Background

If a process is already in execution, such as the tar command example below, simply press Ctrl+Z to stop it then enter the command bg to continue with its execution in the background as a job.

You can view all your background jobs by typing jobs . However, its stdin, stdout, and stderr are still joined to the terminal.

$ tar -czf home.tar.gz . $ bg $ jobs

Run Linux Command in Background

You can as well run a Linux process in the background using the ampersand, & sign.

$ tar -czf home.tar.gz . & $ jobs

Start Linux Process in Background

Take a look at the example below, although the tar command was started as a background job, an error message was still sent to the terminal meaning the process is still connected to the controlling terminal.

$ tar -czf home.tar.gz . & $ jobs tar: ./.config/etcher: Cannot open: Permission denied 

Linux Process Running in Background Message

Keep Linux Process Running After Logout

We will use the disown command, which is used after the process has been executed and put in the background, its work is to remove a shell job from the shell’s active list jobs, therefore you will not use fg , bg commands on that particular job anymore.

In addition, when you close the controlling terminal or log out, the job will not hang or send a SIGHUP to any child jobs.

Let’s take a look at the below example of using the diswon bash built-in function.

$ sudo rsync Templates/* /var/www/html/files/ & $ jobs $ disown -h %1 $ jobs

Keep Linux Process Running After Closing Terminal

You can also use the nohup command, which also enables a process to continue running in the background when a user exits a shell.

$ nohup tar -czf iso.tar.gz Templates/* & $ jobs

Put Linux Process in Background After Closing Shell

You might also like:

Detach a Linux Process From Terminal

Therefore, to completely detach a process from a controlling terminal, use the command format below, this is more effective for graphical user interface (GUI) applications such as Firefox:

In Linux, /dev/null is a special device file that writes off (gets rid of) all data written to it, in the command above, input is read from, and output is sent to /dev/null.

You might also like:

As a concluding remark, provided a process is connected to a controlling terminal, as a user, you will see several output lines of the process data as well as error messages on your terminal. Again, when you close the controlling terminal, your process and child processes will be terminated.

Importantly, for any questions or remarks on the subject, reach us by using the comment form below.

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Parted Command in Linux

TLDR Man Pages for Linux Commands

apt-get Command Examples

Ubuntu apt-cache Commands

apt Command Examples

Funny Linux Commands

10 thoughts on “How to Run Linux Commands in Background and Detach in Terminal”

Hey, Thanks for this little Linux tip, useful for me to make my Linux commands run in the background… Reply

Читайте также:  Просмотр dwg файлов линукс

Without the “disown” you’ll get the following output in the source terminal after the given program is closed: “[1]+ Done firefox /dev/null” This only happens the next time something is put out to the terminal’s output stream. So, the next time command runs that writes to the output stream. To test this, run it without “disown”, close firefox, and then run “ls”. Reply

I see this is an old post but I think my question is sort of in the ballpark. what I would like to do is open the console of the process that was started with systemd with a no GUI flag. what this pertains to the Minecraft server. so I made a systemd service file and the execution command is “java -jar server.jar nogui” now once the system is up and running, I would like to open the GUI or console of the Minecraft server to issue commands without having to open a Minecraft launcher like you would play the game. I know I have done something like this in the past on other platforms but I just don’t know enough about Minecraft/java to make this happen. I am using ubuntu 18.04 and the current version of the Minecraft is 1.15.2 Reply

How to see the jobs running in the previous session? I have submitted jobs in nohup and logged out. After logging again if i wanna see the jobs and if I wanna kill, how to do so Reply

@Bala You can run this command:
ps -fu username #replace username with your real username
OR
ps -x Reply

Источник

Send a Process to Background Linux

When working with graphical desktop environments, we rarely worry about background processes. If we have a process running in the foreground, we can quickly spawn another terminal window and continue with our work.

However, if you are in a raw terminal shell such as SSH, you will often feel concerned about processes that occupy and block the shell until they are completed, especially on long-running jobs. That is where the concept of background and foreground processes comes into play.

This tutorial will discuss what background and foreground processes are, including creating and managing them in Linux.

What is a Process?

Allow me to start at the basic level: what is a process?

In Linux, a process is an instance of a program. Typically, this means any command or executable in a shell is a process.

There are mainly two types of processes:

Foreground processes are mainly typical applications that we launch and interact with them. An example would be the nautilus file manager in Gnome. In most cases, we can start foreground processes from the shell or the desktop environment.

On the other hand, background processes run in the background and require no input or interaction from the user. An example would be any typical Linux daemon.

How to Run a Process in the Background

Suppose we have a process that, while running, occupies the shell session and hinders us from executing commands until it exits.

Читайте также:  Remove files in linux by date

For example, if we run the Firefox browser in the shell, it will occupy the session until process termination.

As you can see, as long as Firefox is running, the shell prompt is unavailable, and we cannot execute any more commands.

To solve this, we can do it two ways:

1: Using an Ampersand (&)

The first method is using the ampersand & sign. This tells the shell to run whatever command precedes the ampersand in the background.

In such a scenario, the process executes in the background and spawns as a new shell prompt allowing us to continue executing commands.

It also gives two numerical identifiers. The first one enclosed in square brackets is the Job ID, while the next one is the process ID.

2: Using CTRL + Z, bg command.

The next method you can use to put a process in the background is to use the shortcut CTRL + Z. This stops the process from blocking the shell. You can then use the bg command to push it to the background.

For example, start by launching Firefox as:

While the process is running, press CTRL + Z. This returns your shell prompt. Finally, enter the bg command to push the process in the background.

How to Show Background Processes

To view and manage processes in the background, use the jobs command in the shell. That will show the background jobs in the current terminal session.

An example output of background jobs:

To bring a process running in the background to the foreground, use the fg command followed by the job id.

For example, to bring the firefox job in the foreground, we use the command:

To put in the background again, press CTRL + Z followed by the bg command.

How to Make a Process Persistent After Shell Dies

When you are running processes in the background, and your shell session dies, all the processes associated with it terminate, which can be problematic, especially if it is an SSH session.

However, this is not too big an issue if you use a terminal multiplexer such as tmux or screen because, in that case, you can simply reattach the session.

However, if you run a shell session without a multiplexer, you can use the nohup command.

The nohup command is immune to hang-ups and can ignore the SIGHUP signal sent to a process.

Hence, if you run a command with nohup, it continues to run even if the shell session accidentally dies.

For example, to run Firefox with nohup, use the command:

This will run the process in the background as persist a shell terminate.

You can run a new terminal session and view the background jobs. You will see the process still running in the background.

Conclusion

In this tutorial, we discussed various ways to run and send processes to the background in Linux. We also covered how to bring a background process to the background and persist hang-up upon shell termination.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

Оцените статью
Adblock
detector