Saturday, January 11, 2020

How to use the screen command on Linux to keep your remote task running when the connection drops

https://net2.com/how-to-use-the-screen-command-on-linux-to-keep-your-remote-task-running-when-the-connection-drops

A lot of Linux users carry out a long running task on a remote destination without taking into account the possibility that the connection might break. In such situations, notwithstanding financial losses for large and small corporations, they will lose their work as well as their time since when they try to reconnect again, the remote task is no longer running.
Fortunately, Linux provides a tool called ‘screen’ which enables these remote sessions to keep running even when the connection drops. Indeed, when a given process is started with this utility, i.e. ‘screen’, it can be detached from the corresponding session and then when the connection is resumed for instance, the process that was originally started from the ‘screen’ is still running and can rejoin the session at a later time, .i.e users can pick up where they left off with their last command or task.
In simple terms, when a user starts a screen session and opens up multiple virtual terminals within that session, the running processes will not be affected by a disconnection event.

What is screen ?

The ‘screen’ utility is a window manager that is used to multiplex a physical terminal between multiple processes or interactive shells. When the screen command is called, a single window will show up where the user can work normally.
Users have the ability to open as many screens as they need, switch between them, list them, kill them, detach them, copy and paste text between them and even reconnect to them.
All windows execute or manage their own programs independently of each other. When a window is not visible, its corresponding programs continue to run even when the full screen session is detached or decoupled from the user’s terminal.

Installing Linux screen

On most Linux distros, the screen utility is already pre-installed. To check if screen is installed on your Linux system , type in the following command for Ubuntu and similar distros:
screen –version

As you can see from the snapshot above, screen is not installed on our Ubuntu 18.04. You can install it by running the apt command :
sudo apt install screen

Start using screen

In order to start your first screen session, type in screen in your terminal:
screen
You will be greeted with the following welcome message :

This will create a screen session, open a new window and finally start a shell in that window.
You may want to read: How to use scp command to transfer files securely using ssh on Linux

List of all currently opened screens

To find the list of all currently opened screens including those that are running in the background, issue the command below in your terminal :
screen -ls

The highlighted number above, .i.e 8585, identifies the PID of the screen session.
If you type in the command below :
ps -e | grep 8585

You will see that it is indeed a screen session process.

Creating a named screen

From the snapshot below, the title of the session is not very descriptive.

Since it is good practice to choose a memorable session name to remember what screen is executing what command, run the command below:
screen -S command_name
Where command_name is the command that would be run by the session. Otherwise you can just choose a name for the session per se as shown below :

When you hit enter, a second screen session will be created. To see the number of currently active screen sessions , use the command you used earlier :

Detaching from a screen

In order to detach yourself from the current screen, Press Ctlr+a d;

Notice that if you quit the terminal altogether and then open it up, you will see that the screen session is still running.
If case you need to quit the terminal, but you want your tasks to continue running in the screen session, you can detach the screen manually in order to reconnect with it later using Ctrl+a d.

Reattaching to a screen

In order to reattach to a screen session, issue the command below in which the PID of the detached session would need to be provided :
screen -r Screen_PID

Once you hit enter, you will automatically get reattached to the screen session you had indicated.
You can also, instead of providing the PID, use the name (or the first few characters of the name) of the screen session, for instance:
screen -r mysec
Where mysec is the beginning of the name of the second screen session called: mysecondcreensession.

How to quit an active session ?

In case you want to kill a screen session, you would need first to display the list of active screen sessions in order to identify the session to terminate :
screen -ls

Now we want to quit session with PID 8585 as shown above. To do this we use the command below :
screen -X -S 8585 quit

where you can see that the screen session with PID 858 was terminated.
Let us now attach to the only screen session left, i.e. with PID 10313 :

In order to kill the current screen session from within it, press Ctrl+a k. You will be prompted with the question :

Go ahead and choose y. This will terminate the screen (actually this will kill the window, but since the screen has one window, it will get terminated).


In order to exit a one-window screen, you could simply type in the exit command which will take you back to the standard bash prompt.

Creating multiple windows in a screen

To create several windows within your existing screen, simply press Ctrl-a followed by c. In order to see the list of windows in your current screen session, press Ctrl-a followed by w.

You can also display the screen windows using ‘Ctrl-a :’

Here you can scroll up or down in order to move between your desired windows and then hit enter once you decide to land on the selected window.

Renaming a window

If you want to rename a window, make sure you are already on the correct window that you want to rename and press Ctrl-a A. This will display the prompt below which enables you to rename the window :

Switching between screen windows

When more than one screen window is opened, you can switch between them by pressing Ctrl-a followed by space or the shell number, .i.e. just press Ctrl-a 0, Ctrl-a 1, Ctrl-a 3 if you have three windows and so forth.
You can also use Ctrl-a N to move to the next screen or Ctrl-a P to return to the previous screen.

Splitting windows

You have the possibility to split your windows instead of switching between them in order to have a bird view of your work. This can be achieved by pressing Ctrl-a followed by S or | for horizontal or vertical splitting. To move between panes, use Ctrl-a followed by tab. Here is how splitting a screen session will look like :

Resuming a disconnected session

In case you want to resume a detached session to continue your work, run the command :
screen -d -r your_detached_ression
This is helpful when you want to continue your work after a disconnection event for instance without losing the detached screen session context (running processes , window output).

How to run a command with screen

When you have a command to that you want to run using screen, issue the command below :
screen -d -m your_command
For instance to run a Python script:, issue the command below:
screen -d -m python your_python_script

The screen configuration file

When you first call screen, it reads its actual configuration parameters from the file /etc/screenrc and ~/.screenrc if it is available. You can alter the default screen settings using the .screenrc file.

Conclusion

In this tutorial, you were introduced to the Gnu screen utility. Now you should be able to use the screen tool, create screen windows, navigate between them, detach and resume screen sessions as well as customize your screen display using the .screenrc file.
To learn more about Gnu screen, visit the screen user’s manual page.

How to copy a file to multiple directories in Linux

https://net2.com/how-to-copy-a-file-to-multiple-directories-in-linux

In this short article, you will learn how to copy one file into many directories. The obvious answer would be to use the cp command but this not is exactly correct since the cp command is rather used to copy several files into one directory. You will see what additional commands and combinations are needed to pull this off. Let’s get started.

The cp command basic syntax

The most basic cp command syntax that is used to copy multiple files into one directory is the following :
cp file_1 /directory_1/
cp file_1 file_2 file_3 /directory_1/
Here is an example :
cp /Documents/FileExample.txt /TextFiles/
cp command

How to copy one file to several directories ?

As mentioned above, the cp command cannot be used to copy one file to multiple folders. The solution would be to use the xargs or GNU parallel commands.

Using xargs

To copy for instance file_1 into the folders directory_1, directory_2, we could proceed as follows:
echo directory_1 directory_2 | xargs -n 1 cp file_1
In the command above, target directories (directory_1,directory_2) are first echoed and then piped out (or fed) as input to the command xargs where:
-n 1 : Instructs xargs to use one argument per command line at a time and forward to the cp command
cp : classic cp command
-v : Allows verbose mode in order to display more details of the copy task
The xargs will execute the cp command two times (i.e. as many target directories as provided in the input) where at each run, it appends the next directory path fed to it from the previous echo command to the end of the standard cp command.
So instead of executing two separate cp commands, we can now use one single command to perform the same task. If the file to be copied exists already in one of the destination folders, the old file will be replaced without prompting the user. In other words, the -i option (interactive) of the cp command cannot be used in conjunction with xargs.
If the file to be copied has a large size and you do not want the destination file (if it exists) to be replaced, you might want to add the -n switch to the cp command in the single line above. This will prevent the destination file from being replaced.
Read: How to use grep command in Linux


Using find

Another alternative to carrying out a copy to multiple destinations is to use the find command as follows :
find directory1 directory2 -exec cp file.txt {} \;
If the target directories have sub-directories and you don’t want to copy the file into them, you would need to add -maxdepth 0 option as follows:
find directory1 directory2 -maxdepth 0 -exec cp file.txt {} \;
This will overwrite or replace every file in directory1 and directory2 with the content of file.txt before copying it. In order not to affect other files in these destination directories, make find aware that it should only act on directories as follows:
find directory1 directory2 -type d -exec cp file.txt {} \;
Read: Linux directories explained

Using loop in a shell

xfAnother solution would be to use a for loop within a one line shell as follows :
for dir in *; do [ -d “$dir” ] && cp /full_path/file.txt “$dir” ; done
This will copy the file /full_path/file.txt to all directories in your current path or location.
for dir in *; do [ -d “$dir” ] && cp -rf /full_path/folder “$dir” ; done
This however will copy the folder /full_path/folder to every sub-folder or sub-directory in your current location.
Read: How to find the largest files on Linux

Using GNU parallel

GNU parallel is a shell utility used for executing tasks or jobs in parallel over one or multiple machines. The basic syntax is as follows:
parallel cp file_name ::: /directory1/ /directory2/
Here is an example on how to use it :
Let’s copy the file /etc/resolv.conf to /directory1/, /directory2/ :
parallel cp -v /etc/resolv.conf ::: /directory1/, /directory2/

Using tee

The tee command allows you to copy one file to multiple destinations. Here is an example on how to perform this:
tee ~/directory1/file1 ~/directory2/file1 < ~/file1
Note that the input that is written by tee will be forwarded to the standard output (stdout). If you don’t like this behavior, you have the possibility to prevent it by rerouting standard output to /dev/null as shown below:
tee ~/directory1/file1 ~/directory2/file1 < ~/file1 >/dev/nul

Conclusion

As you have seen, there are many ways to copy a file to multiple directories. Most of these solutions make use of the cp command which cannot perform this feat on its own.