We are using centos. As soon as the server is restarted and the OS is fully loaded, we would like to have a certain user ("foo") start three or four scripts with screen.

For example,

screen -d -m -S script1 forever -o script1.log -e script1.log -l script1.log -c php /path/to/script1.php

What's the best way to achieve this using CentOS? I would prefer to avoid having to dig into init.d too much.

Using systemd, I try to inspect the service ("notify") and see this:

   Loaded: loaded (/etc/systemd/system/notify@.service; enabled)
   Active: failed (Result: exit-code) since Sun 2016-03-20 15:08:04 EDT; 14s ago
  Process: 1690 ExecStop=/usr/bin/screen -S notify -X quit (code=exited, status=1/FAILURE)
  Process: 941 ExecStart=/usr/bin/screen -d -m -S notify forever -o notifyout.log -e notifyerr.log -l notifyforever.log -c php /path/to/script/notify.php (code=exited, status=0/SUCCESS)
 Main PID: 946 (code=exited, status=0/SUCCESS)

Why is it getting stopped? How can I begin to debug this?

  • 1
    "We are using centos." which version? Also, why the aversion to starting these scripts using the init system, are there requirements leading you to use screen rather than your system's init setup or another process manager – Steven D Mar 20 '16 at 17:27
  • apologies, I hit enter too fast. Just updated my comment with a more sensible question :D – Steven D Mar 20 '16 at 17:29
  • @StevenD we are using CentOS Linux release 7.1.1503 (Core). I like the idea of using 'screen' because if it seems to work well with the 'forever' tool, and if we need to restart them due to a modification, screen seems easier to keep checking. However, I am completely open minded to choosing the approach that works best. – Zack Burt Mar 20 '16 at 17:30
  • Is this the forever program you are referring to? github.com/foreverjs/forever – Steven D Mar 20 '16 at 17:41
  • Yep. Forever will monitor the script and restart if it crashes. It will also send all the output (stdout and stderr) to log files. – Zack Burt Mar 20 '16 at 17:50

Since this is CentOS 7, you should use a systemd service to start the service. You can even have it run inside a screen. From the archlinux wiki:

Create a file: /etc/systemd/system/screen@.service


ExecStart=/usr/bin/screen -DmS autoscreen
ExecStop=/usr/bin/screen -S autoscreen -X quit


Then enable it with systemctl enable screen@USERNAME.service If you don't plan on making it so it can be run under any user, you can get rid of the @ in the filename, and the @USERNAME in the unit name when enabling it, and hard-code the user in the unit file.

| improve this answer | |
  • Thanks... Updated my post with some additional debugging info – Zack Burt Mar 20 '16 at 19:10

I edited /etc/rc.local and added this


Then I added /etc/init.d/start_bg_scripts:

/usr/bin/screen -d -m -S script1 forever --minUptime 1 --spinSleepTime 1 -o script1out.log -e script1err.log -l script1forever.log -c php /path/to/script.php

The --minUptime 1 and --spinSleepTime 1 means that they keep trying to reconnect until the rest of the system services (e.g. mysqld) become available

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.