-1

I'm writing a Linux shell script in which I need to start a new screen session, run a node.js server in the screen, then detach from the screen so that my server runs in the background.

Right now, these are the commands I run manually to do this:

screen
node server.js
[detach screen]

However, I need a way to automate this via the script, and if I just use the above commands in a shell script, it creates the screen and gets stuck there. How can I pass the "node server.js" command to the screen command?

EDIT:

Based on the suggested answer I have a script that works, except that I need to manually create a screen and detach from it before I run it. I tried adding screen -d -m as the first line to create a detached screen, but the script hangs after that line.

tempfile=$(mktemp)
indices=`tail -1 debug.log`
cat > $tempfile <<EOF
node server $indices
EOF
screen -X readbuf $tempfile 
screen -X paste . 
rm -f $tempfile

How can I create and detach a screen with the script?

This didn't work either: screen screen -d

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sara
  • 11
  • 1
  • 2
  • Possible duplicate of http://stackoverflow.com/questions/20880883/using-screen-in-bash-script – michaelrmcneill Jul 11 '14 at 21:31
  • That helped me a bunch, except I don't want to run the command in a detached screen. – Sara Jul 13 '14 at 00:42
  • So just to make sure I understand, you're looking to run the command in an attached screen, then, when complete, detach from the screen? – michaelrmcneill Jul 13 '14 at 00:50
  • This is going to run at reboot, so I'm looking to run the command in a new screen. So basically I want to create a screen, run the command in it, then detach. – Sara Jul 13 '14 at 00:55

2 Answers2

2

It is as simple as this: screen -md node server.js This requires the command to run as a service (as it does), otherwise the screen stops immediately.

To optionally also set a name for the session (e.g. "session-name"): screen -mdS session-name node server.js You can then attach to the screen with: screen -rd session-name If you want to redirect all output to a file, you can do like this: screen -mdS session-name bash -c 'node server.js &> output.log' You can then monitor the output with e.g.: tail -f output.log

You can list your running screens with: screen -ls or screen -list

Example: Start a Python3 web server in a detached screen

Start a Python3 web server listening on port 8000 that serves files in the current directory, in a named detached screen: screen -mdS my-web-server python3 -mhttp.server Or, with logging to a file: screen -mdS my-web-server bash -c 'python3 -mhttp.server &> output.log' For Python 2.x it looks like this: screen -mdS my-web-server bash -c 'python -mSimpleHTTPServer &> output.log'

knuth
  • 683
  • 5
  • 7
0

EDIT: Try this:

tempfile=$(mktemp)
cat > $tempfile <<EOF
node server.js
EOF
screen -S SessionName -X readbuf $tempfile; screen -RdS SessionName
screen -X paste .
rm -f $tempfile

It should create the temp file, create a screen called SessionName and runs the commands, then delete the temp file. Let me know if that works.

michaelrmcneill
  • 1,163
  • 1
  • 10
  • 23
  • Thanks Michael, but it still has the same problem. When I run it, it does start a new screen but it gets stuck there. But now when I detach from the screen, the command prompt says "No screen session found.\n [detached from 6930.SessionName]" whereas before it didn't say anything. Also, what does -RdS do? – Sara Jul 13 '14 at 03:05