How to create a screen executing given command?

前端 未结 7 1593
星月不相逢
星月不相逢 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: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.

提交回复
热议问题