PDA

View Full Version : SLES 11 SP4 executing script as last thing after boot or reboot



ron7000
17-Nov-2015, 17:51
what and how is the best way to accomplish having a simple script execute once whenever the system boots?
This would be for a cold boot from a powered off state, or after a reboot or init 6.
And i would want the script to be the very last or as close to last thing that happens after boot/reboot.

my script is just a few lmgrd commands to kick off some flexlm licenses, or echo the date and time to a text file of my choosing.
ideally i would want the script to execute the lmgrd commands as some other user besides root.

ron7000
17-Nov-2015, 20:07
is the best/only way to do this by setting up the link in /etc/init.d/rc5.d/ such as S90_myscript and
have it link to /etc/init.d/myscript and do a chkconfig --add /etc/init.d/myscript ?

I was hoping there was some .local file I could place a call to my script,
rather than editing a full blown service file for start/stop/restart and all that which i'll never need.

p.s. I am not asking for any code to be written :rolleyes:

jmozdzen
17-Nov-2015, 22:23
Hi ron7000,

is the best/only way to do this by setting up the link in /etc/init.d/rc5.d/ such as S90_myscript and
have it link to /etc/init.d/myscript and do a chkconfig --add /etc/init.d/myscript ?

I was hoping there was some .local file I could place a call to my script,
rather than editing a full blown service file for start/stop/restart and all that which i'll never need.

p.s. I am not asking for any code to be written :rolleyes:

but you'll get some ;)

From /etc/init.d/rc (which is handling start/stop of run levels):

#
# Start the user defined resource control script which
# should be executed _after_ runlevel is reached.
#
if test -f /etc/init.d/after.local ; then
echo -n "Master Resource Control: "
echo "Running /etc/init.d/after.local"
/bin/sh /etc/init.d/after.local
rc_status -v1 -r
splashtrigger "after.local"
fi

So you could put your code into /etc/init.d/after.local.

Regards,
Jens

ron7000
18-Nov-2015, 06:01
I had come across after.local doing a web search and it was stated that,
"This is executing scripts just after the initial machine boot, not the end of the init boot process"
This statement had a date of 2012 though,

so here is my after.local file


#!/bin/bash

echo "ronron running after.local"
echo -n "whoami = "
whoami
echo "su to ron2"
su ron2
echo -n "whoami = "
whoami

date >> /tmp/after.local.time.txt


below is a cleaned up except from bottom of /var/log/boot.msg



Starting smartd done
Starting Firewall Initialization (phase 2 of 2) SuSEfirewall2: done

Master Resource Control: Running /etc/init.d/after.local
ronron running after.local
whoami = root
su to ron2
Warning: no access to tty (Inappropriate ioctl for device).
Thus no job control in this shell.
HOSTNAME: Undefined variable.
linux />

so it seems like using /etc/init.d/after.local is exactly what i need.

still have to figure out how to not be root and use some other account to kick off the things i want to,
my use of the su command in the script did not work.

ron7000
18-Nov-2015, 06:19
got it, needed to do this in /etc/init.d/after.local:



echo -n "su -c whoami ron2: whoami = "

/bin/su -c whoami ron2



that produced the following in /var/log/boot.msg



Master Resource Control: Running /etc/init.d/after.local
ronron running after.local
whoami = root
su -c whoami ron2: whoami = ron2
doneMaster Resource Control: runlevel 5 has been reached

jmozdzen
18-Nov-2015, 10:27
Hi ron7000,

> got it, needed to do this in /etc/init.d/after.local: [...]

yes, I was about to reply that "su" will start a sub-shell and that only commands within that sub-shell are running in the changed security context. If you have more than a single command to execute, create a wrapper script and call that instead (instead of creating a long, long "-c" parameter).

Also, during boot the script is not running with a connected tty, hence the "Warning: no access to tty (Inappropriate ioctl for device)." message, as your original call to "su" tried to create an interactive shell (which is not meaningful without a tty ;) )

Regards,
Jens