How to run my script after SuSE finished booting up?

I want my server to notify me (via growl) that the (re)booting finished. I have the php file that sends out the growl ready but I do not know what file I should add the line in.

What file I need to add my code so it is run only after booting of SuSE finished?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

(open)SUSE uses /etc/init.d/after.local for this purpose. Just add the commands you need to be executed into that file. Note that this works fine with SystemV init, but with systemd this would need AFAIK need to be solved differently.

Method 2

Newer syntax for Suse Linux Enterprise 11 SP2 (and openSUSE ?)

The best way would be to create a shell script that will call your PHP script. This shell script should have in its header the following comment:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          nothing
# Required-Start:    $all
# Default-Start:     3 5
# Default-Stop:      4
# Short-Description: single_line_description
# Description:       multiline_description
### END INIT INFO

You can find a typical template (with loads of explanatory comments) in /etc/init.d/skeleton. This template includes the necessary code to hook your PHP script. You will see a start case where you would have to call your PHP script.
I have discarded a number of optional parameter in the header as it does not appear that you would need them.

Important
It is important to use the skeleton at least the case statement (see init scripts actions), and to implement the start case at least. In the start case, that is where to call your script.

You can find here a small script example that will be called at the end of a boot: see my Gist. I put an invalid run level for the Default-Stop, but somehow the script is still called during shutdown. Anyway, the code in the “stop” case is executed, not the one in the “start” case while shutting down.

Once you have written your script, copy it to /etc/init.d let’s assume that your init script is called boot-notification, then you would do (as root):

chown root:root boot-notification
chmod 0750 boot-notification
mv boot-notification /etc/init.d/

Then you need to “register” the script in the init system. You will use the insserv command (again as root) or you can use YaST:

insserv boot-notification

Then you can further check that the script is one of the last to be run by looking into each init level. If you chose only runlevel 3, then you could do this:

ls -l /etc/init.d/rc3.d/S*

This will return a list of links to init scripts. The link to your script should be at the end (or near it) of the list.

Note: If you want to play around with the more dynamic way of writing init scripts, I would advise reading these 2 pages:

Method 3

For OpenSUSE 12.2 (Mantis), the script for “after local” should be /etc/init.d/after-local , and you may need to enable it via systemctl, like this…

To enable /etc/init.d/after-local,

systemctl enable after-local.service

To check status of /etc/init.d/after-local,

systemctl status after-local.service

Method 4

To add a new init script is quite straight forward on Suse.

The best way would be to create a shell script that will call your PHP script. This shell script should have in its header the following comment:

#!/bin/sh
#chkconfig: 35 99 00
#description: Notify of boot completion

You can find a typical template (with loads of explanatory comments) in /etc/init.d/skeleton.compat (that one supports the chkconfig syntax, you have also 7etc/init.d/skeleton which supports newer LSB standard, but it is a bit more complicate to explain you how to set it up, though it is more powerful). This template includes the necessary code to hook your PHP script. You will see a start case where you would have to call your PHP script.

The important number I gave you are on the chkconfig line.

  • 35: means that this script will be called in either init 3 (console mode, usual for server) or init 5 (graphical mode, more common for desktop)
  • 99: is the priority in either init 3 or 5. It means that it will be called last. Note that some other boot scripts could be called with a priority 99.
  • 00: is the priority for shutdown/reboot. You could also have a notification as soon as the system is going down.

Once you have written your script, copy it to /etc/init.d let’s assume that your init script is called boot-notification, then you would do (as root):

# chown root:root boot-notification
# chmod 0750 boot-notification
# mv boot-notification /etc/init.d/

Then you need to “register” the script in the init system. You will use the chkconfig command (again as root):

# chkconfig --add boot-notification

Check that this was taken into account properly:

# chkconfig boot-notification
boot-notification on

If you see on, it is good!
Then you can further check that the script is one of the last to be run by looking into each init level. If you chose only runlevel 3, then you could do this:

$ ls -l /etc/init.d/rc3.d/S*

This will return a list of links to init scripts. The link to your script should be at the end (or near it) of the list.

Note: If you want to play around with the more dynamic way of writing init scripts, I would advise reading these 2 pages:

Method 5

I found that there are 2 ways to do that:

  1. First and foremost is to open terminal and type:

sudo crontab -e

then type

@reboot sudo <command_to_execute_at_startup>

  1. Secondly you can create a service file using this method


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x