1

How can I make a script run on startup as root and also creating a screen for the script?

I think I could just add

screen -S name

to the top of the script to achieve the screen stuff, but I'm not sure.

Is this all possible to do in crontab?

Jonathan
  • 297

1 Answers1

2

We use (in a perl script that is started in /etc/rc.local with

su {username} -c '/usr/local/bin/schermen start'

The script has a "start","stop" and "restart" options.

):

screen -d -m -S {screen_name} {$program_to_start}

The man page has a notice om using -d -m:

-m   causes  screen  to  ignore the $STY environment variable. 
With "screen -m" creation of a new session is enforced, 
regardless whether screen is called from within another screen session 
or not. This flag has a special meaning in connection with the `-d' option:

-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.

Extra:

-S sessionname When creating a new session, this option can be used 
to specify a meaningful name for the session. 
This name identifies  the  session  for  "screen -list" 
and "screen -r" actions. It substitutes the default [tty.host] suffix.
Rinzwind
  • 299,756
  • So what you're saying is create a script that in your example is named schermen and in that script have the screen stuff etc? – Jonathan Aug 10 '17 at 09:03
  • As an example. "schermen" is Dutch for "screens" ;-) Our script is a bit big since we need to open about 10 screens for a 50 instances (and we decided to have each have their own id) (we can start/stop/restart 1, all on 1 instance or all instances remotely). – Rinzwind Aug 10 '17 at 09:43
  • Aah, I see! I tried adding the stuff to my /etc/rc.local file but when I reboot it doesn't work.. :/ My rc.local -> https://pastebin.com/bzCdJYEQ (ecoround is the user) – Jonathan Aug 10 '17 at 10:35
  • Tried manually executing rc.local and it tells me in terminal: ./rc.local: line 1: !/bin/sh: No such file or directory, this is probably why it won't work. Any idea why it's telling me this? – Jonathan Aug 10 '17 at 10:43