View Full Version : Running a command at boot
Bung
March 12th, 2009, 00:32
Hello,
I would like to run a certain command at boot on my freebsd VDS;
screen -d -m -S triplea /home/triplea/data/triplea_1_0_2/run-server.sh
I have tried to create a file in /usr/local/etc/rc.d/ named triplea with the content
#!/bin/sh
screen -d -m -S triplea /home/triplea/data/triplea_1_0_2/run-server.sh
That wouldnt work, and the rc.d pages i saw in the handbook were either too complicated or confusing for my purpose.
I was also suggested to try crontab. So I did the following
#crontab -e -u triplea
and added the line
@reboot /usr/share/bin/screen -d -m -S triplea /home/triplea/data/triplea_1_0_2/run-server.sh
However after rebooting, it didnt work. Here is the tail end of /var/log/cron:
Mar 11 21:50:00 allanr2 /usr/sbin/cron[98390]: (root) CMD (/usr/libexec/atrun)
Mar 11 21:50:00 allanr2 /usr/sbin/cron[98404]: (root) CMD (/usr/local/ispmgr/sbin/watchdog)
Mar 11 21:51:04 allanr2 crontab[99580]: (root) BEGIN EDIT (triplea)
Mar 11 21:52:11 allanr2 crontab[99580]: (root) REPLACE (triplea)
Mar 11 21:52:11 allanr2 crontab[99580]: (root) END EDIT (triplea)
Mar 11 21:52:13 allanr2 crontab[99848]: (root) BEGIN EDIT (triplea)
Mar 11 21:52:16 allanr2 crontab[99848]: (root) END EDIT (triplea)
Mar 11 21:52:44 allanr2 /usr/sbin/cron[322]: (triplea) CMD (/usr/local/bin/screen -S triplea -md /home/triplea/data/triplea_1_0_2/run-server.sh)
Mar 11 21:55:00 allanr2 /usr/sbin/cron[944]: (root) CMD (/usr/libexec/atrun)
Mar 11 21:55:00 allanr2 /usr/sbin/cron[988]: (operator) CMD (/usr/libexec/save-entropy)
Well thats all the info Ive got and everything Ive tried. Id really appreciate any help with this, if you need any information, please dont hesitate to ask.
Thanks for your time,
Bung
Speedy
March 12th, 2009, 01:41
Try using full path for every command. If you need to keep a process alive, svscan does good job.
Bung
March 12th, 2009, 01:42
Ah so you mean in the file i tried to add to /usr/local/etc/rc.d/ ... i forgot about that and will try it, Ill also look into svscan, thanks.
Speedy
March 12th, 2009, 01:44
#!/bin/sh
/path/to/triplea /home/triplea/data/triplea_1_0_2/run-server.sh
This is what I meant. Don't see what good screen would do here.
Edit: This triplea is some sort of command, right?
Bung
March 12th, 2009, 01:54
Ahm, ok so now i have a file /usr/local/etc/rc.d/triplea and by typing in # /usr/local/etc/rc.d/triplea ... it actually works. How exactly do I get the server to run it on bootup?
Bung
March 12th, 2009, 01:55
triplea is just the username... it is also the name of the program. run_server.sh starts a java application. screen lets it run in the background for ease of loggin in/out to look at stats and run other commands.
Speedy
March 12th, 2009, 02:10
All the stuff in /usr/local/etc/rc.d is executed at bootup, executable bit has to be set of course.
Bung
March 12th, 2009, 02:23
OK well,
this works just fine; % /usr/local/etc/rc.d/triplea
where the contents are
#!/bin/sh
/usr/local/bin/screen -S triplea -dm /home/triplea/.../run_server.sh
The permissions of the file are the same as all other files in that folder
-r-xr-xr-x 1 root wheel 96 Mar 12 00:48 triplea
And yet on bootup, it does not run :'(
Mel_Flynn
March 12th, 2009, 07:35
#!/bin/sh
# PROVIDE triplea
# REQUIRE LOGIN DAEMON mountcritlocal
. /etc/rc.subr
name=triplea
rcvar=`set_rcvar`
command='/usr/local/bin/screen'
load_rc_config $name
start_cmd="${name}_start"
stop_cmd="${name}_stop}"
restart_cmd="${name_restart}"
: ${triplea_enable}="YES"
: ${triplea_session}="triplea"
: ${triplea_args}="/home/triplea/data/triplea_1_0_2/run-server.sh"
triplea_start () {
${command} -d -m -S ${triplea_session} ${triplea_args}
}
triplea_stop() {
${command} -D -S ${triplea_session}
}
triplea_restart() {
${command} -S ${triplea_session} -r
}
run_rc_command "$1"
Speedy
March 12th, 2009, 10:43
If you set rcvar then don't forget to enable your daemon in rc.conf. Your script may need .sh extension to work.
DutchDaemon
March 12th, 2009, 10:51
Is it even possible for screen to get executed (i.e. attached to a tty and sent to the background) at boot time?
Speedy
March 12th, 2009, 11:03
Probably it should work, /usr/local/etc is executed after main in /etc. Just guessing. (I think there is description of boot process in the handbook?)
Mel_Flynn
March 12th, 2009, 16:33
Is it even possible for screen to get executed (i.e. attached to a tty and sent to the background) at boot time?
From screen(1):
-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.
If you set rcvar then don't forget to enable your daemon in rc.conf. Your script may need .sh extension to work.
No and no:
This script is rcNG and doesn't need a .sh extension.
: ${triplea_enable}="YES" means, set default to enabled.
Speedy
March 12th, 2009, 16:48
Point taken. For sake of truth, my comment about sh extension was about non-rcNG scripts.
I still see no good running it inside a screen session. Doesn't hurt, but what's the fruit? Monitoring output? Same can be done by sending the output into a log and watching it with tail -f.
Bung
March 12th, 2009, 18:09
Thank you for your time in getting that script Mel Flynn, and thanks to speedy and Dutch.
@speedy, the reason I run it in screen is, as I mentioned before, its a java application that has its own interface. We can run sql commands (to set admin, etc) on the user database and other things. I did not write the application, its a gaming lobby for the open source Axis & Allies java game TripleA.
@Mel Flynn, thanks again, that script was definately over me head to conceive. I gave it a try this morning and found two things.
The first thing was that at first try, of % /usr/local/etc/rc.d/triplea start, it stopped at an improper usage of screen. That is, it showed the screen help info as if it was passed improper variables. By modifying command="" to include the entire "/usr/local/bin/screen -S triplea -dm /home/yadda/yadda" it would work after setting the triplea_start() function to just {$command}.
Once that problem was fixed, I again ran % /usr/local/etc/rc.d/triplea start and it output; "WARNING: no shebang file in /usr/local/bin/screen" which I understand to mean it thinks its a shell script instead of a program (from google.) However, the good news is that this started up fine and my screen session was a go! The bad news is that when rebooting the VDS, it did not start. To be exact, screen shows up in the processes list momentarily, then disappears.
I will continue to tinker with it when I get home from school this evening. Thanks again for the help so far fellas :)
Bung
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.