How to create a screen executing given command?

前端 未结 7 1572
星月不相逢
星月不相逢 2020-12-07 18:07

i\'m fairly new in *nix. Is there a way to create a screen, which will immediately execute a given command sequence (with their own arguments)? Two hours of googling yields

相关标签:
7条回答
  • 2020-12-07 18:16

    You create a screen with a name and in detached mode:

    screen -S "mylittlescreen" -d -m
    

    Then you send the command to be executed on your screen:

    screen -r "mylittlescreen" -X stuff $'ls\n'
    

    The $ before the command is to make the shell parse the \n inside the quotes, and the newline is required to execute the command (like when you press enter).

    This is working for me on this screen version:

    $ screen -v

    Screen version 4.00.03jw4 (FAU) 2-May-06

    Please see man screen for details about the commands.

    0 讨论(0)
  • 2020-12-07 18:17

    I wanted to launch remote screens from within a bash script with some variables defined inside the bash script and available inside screen. So what worked for me was

    #!/bin/bash
    SOMEVAR1="test2"
    # quit existing if there is one running already, be careful
    screen -D -RR test1 -X quit || true
    screen -dmS test1
    screen -r test1 -p 0 -X stuff $"echo ${SOMEVAR1} ^M"
    

    Where the return character, ^M, you need to enter using vim as

    i CTRL-V ENTER ESCAPE
    
    0 讨论(0)
  • 2020-12-07 18:25

    I think that you can use this

    function exec_in_screen() {
      name=$1
      command=$2
      screen -dmS $name sh; screen -S $name -X stuff "$command\n";
    } 
    

    Then...

    exec_in_screen "test" "ls"

    0 讨论(0)
  • 2020-12-07 18:31

    screen -dmS screen_name bash -c 'sleep 100'

    This will create new screen named screen_name. And inside the screen it will sleep for 100 seconds.

    Note that if you type some command in place of sleep 100 which terminates immediately upon execution, the screen will terminate as well. So you wont be able to see the screen you just created

    0 讨论(0)
  • 2020-12-07 18:37

    The problem is that using the 'exec' screen command does not start a shell. 'cd' is a shell builtin, so you need a shell for it. Also, you need a shell that remains running so that screen does not terminate.

    You can use the -X option to screen to send commands to a running screen session, and the 'stuff' command to send keystrokes to the current window. Try this:

    screen -dmS new_screen sh
    screen -S new_screen -X stuff "cd /dir
    "
    screen -S new_screen -X stuff "java -version
    "
    

    Yes, you need to put the quotes on the next line in order for the commands to be executed.

    0 讨论(0)
  • 2020-12-07 18:39

    Another approach

    First line cd to the your directory. Second line start a new screen session named new_screen with bash. Third line executing java -version

    cd /dir
    screen -dmS new_screen bash
    screen -S new_screen -p 0 -X exec java -version
    
    0 讨论(0)
提交回复
热议问题