Linux run command in screen

Run script in a screen

I want to run a bash script in a detached screen. The script calls a program a few times, each of which takes too long to wait. My first thought was to simply open a screen and then call the script, but it appears that I can’t detach (by ctrl-a d ) while the script is running. So I did some research and found this instruction to replace the shebang with following:

#!/usr/bin/screen -d -m -S screenName /bin/bash 

But that doesn’t work, either (the options are not recognized). Any suggestions? PS It occurs to me just now that screen -dmS name ./script.sh would probably work for my purposes, but I’m still curious about how to incorporate this into the script. Thank you.

3 Answers 3

The shebang line you’ve seen may work on some unix variants, but not on Linux. Linux’s shebang lines are limited: you can only have one option. The whole string -d -m -S screenName /bin/bash is passed as a single option to screen , instead of being passed as different words.

If you want to run a script inside screen and not mess around with multiple files or quoting, you can make the script a shell script which invokes screen if not already inside screen.

#!/bin/sh if [ -z "$STY" ]; then exec screen -dm -S screenName /bin/bash "$0"; fi do_stuff more_stuff 

@Lordofdark That’s the script name. The script invokes screen which invokes /bin/bash which invokes the script again.

According to the screen man pages:

  • screen -d -m Start screen in detached mode. This creates a new session but doesn’t attach to it. This is useful for system startup scripts.
  • -S sessionname Set the name of the new session to sessionname.

So when I ran the command you provided: screen -dmS name ./script.sh

Screen starts a window called name and automatically runs that script.sh. To get back into there to see the status you would simply type: screen -r test

Now with Ubuntu 14.04, the commands are slightly different. Try:

Now for running the script, you will need to go to their config file to do so:

Once there, scroll down to the bottom and you will see:

# Example of automatically running some programs in windows on screen startup. # # The following will open top in the first window, an ssh session to monkey # in the next window, and then open mutt and tail in windows 8 and 9 # respectively. # # screen top # screen -t monkey ssh monkey # screen -t mail 8 mutt # screen -t daemon 9 tail -f /var/log/daemon.log 

This is the section where you will need to add the script name to run and that should allow you to do everything you needed from screen.

Читайте также:  Установить репозиторий debian astra linux

Источник

How to execute a command in screen and detach?

How can I get screen to execute a command and then detach (That is, automatically in a single script without further input beyond initially starting the script)? e.g. I run myscript.sh and it automatically starts a screen session, executes a command, then detaches.

It is important to note that for many of these answers, screen will automatically terminate your screen when the command is done running and the program exits. So, you might think it didn’t work, but you are likely just not seeing it because it terminated. That is why there is a sleep or exec command in answers below, to force screen to not terminate.

8 Answers 8

-d -m
Start screen in detached mode. This creates a new session but doesn’t attach to it. This is useful for system startup scripts.

Somehow your command wasn’t found, or isn’t working correctly in the automatically-created screen environment. Try just doing screen yourcommand without the -d and -m and see how that goes first.

You’re sort of right. screen terminates when the command finishes, contrary to my expectations. But your answer does work.

You can change that by enabling the zombie option in screen. Put zombie xy in your ~/.screenrc . It should also be possible to enable it for one session only b putting zombie xy in another file and using -c file but for some reason that’s not working when I try it. Or just use sh -c ‘yourcommand;while :;do sleep 9999; done’

@AlanCurry, Nope, this doesn’t work for me either, even though the command runs perfectly (and takes several hours) when run in a screen manually.

To run a single command in screen and detach, you may try:

To run multiple commands, try:

screen -dm bash -c "sleep 10; myscript.sh" 

Please note that when a program terminates, screen (per default) kills the window that contained it.

Читайте также:  Linux folder with spaces

If you don’t want your session to get killed after script is finished, add exec sh at the end, e.g.:

screen -dm bash -c 'sleep 5; exec sh' 

To list all your sessions, try:

This worked for me on Ubuntu 16.04. In addition to name your session so you can return to it later, add -S sessionname : screen -dmS MyLongRunningScript bash -c «. » .

@kenorb, the problem is that it only worked with single quotes. Let’s say: ‘for k in `seq $i $j`; do echo $k; done’ , where $i and $j are from the parent script.

This is a robust answer. I needed bash -c . I can only assume that when I called screen -dm head foo > bar it wrote screen -dm head foo to bar . screen -dm bash -c «head foo > bar» fixed that.

In order to start new session in background with name ‘sleepy’

screen -S sleepy -dm sleep 60 

In order to kill ‘sleepy’ session

@Toolkit The issue is that you have the command in quotes and so it was treated as one large command. Obviously we can’t take it out of quotes because of the semicolon. To solve this, execute the command like so: screen -S sleepy -dm bash -c «cd myfolder;sleep 60»

screen -dmS screen_session_name bash -c 'echo "doing stuff"; exec bash' 

it happen to me when I pressed control c (sig int) to exit my program. it exits all the way from all bash. so I found this to catch SIGINT. and prevent exit from last bash. (need to type exit to exit)

screen -dmS "screenNameHere" bash -c "trap 'echo gotsigint' INT; cd /mydir ; my_command_here; bash" example: screen -dmS "status_updates" bash -c "trap 'echo gotsigint' INT; cd /opt/status_update ; forever index.js ; bash" 

I find it useful to use cron to run nodejs programs on startup. and to run the screen at boot time. in cron there are special events syntax @reboot event

to edit cron, execute: crontab -e then type @reboot screen -dmS "screenNameHere" bash -c "trap 'echo gotsigint' INT; cd /mydir ; my_command_here; bash" 

Источник

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? **

Читайте также:  Мониторинг узлов сети linux

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.

Источник

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