Execute command at shutdown and boot in Suse Linux

Learn how to setup commands or scripts to execute at shutdown and boot in Suse Linux

Execute a command at shutdown and boot in Suse Linux

In this article, we will walk you through the procedure to schedule scripts at shutdown and boot in Suse Linux. Many times, we have a requirement to start certain applications or services or script after server boots. Sometimes you want to stop application or service or run the script before the server shuts down. This can be done automatically by defining commands or scripts in certain files in Suse Linux.

Application auto start-stop along with OS reboot

Let’s walk through steps to configure the custom applications to auto-start and stop along with Linux reboot. Create a file with a custom name (e.g autoapp) in /etc/init.d as below –

#!/bin/sh
### BEGIN INIT INFO
# Provides: auto_app
# Required-Start: $network $syslog $remote_fs $time
# X-UnitedLinux-Should-Start:
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Start and stop app with reboot
# Description: Start and stop custom application with reboot
### END INIT INFO#
case "$1" in
"start")

        su - appuser -c "/app/start/command -options"
        echo "Application started"
        ;;
"stop")
        su - appuser -c "/app/stop/command -options"
        ;;
*)
        echo "Usage: $0 { start|stop }"
        exit 1
        ;;
esac
exit 0

Make sure you copy all the above text including INIT block at the beginning of the file. Edit appuser and app commands under start and stop blocks.

Set executable permission on this file.

The next step is to identify this file as a service using chkconfig. Use filename as a service name in the below command.

root@kerneltalks # chkconfig --add autoapp

Now enable it to be handeled by systemctl

root@kerneltalks # systemctl enable autoapp

And you are done. Try to start and stop the application using systemctl command to make sure your configuration is working fine. To rule out any permission issues, script entries typo, etc.

root@kerneltalks # systemctl stop autoapp
root@kerneltalks # systemctl start autoapp

If systemctl is properly starting and stopping application as expected then you are all set. Final test you can do by rebooting your server and then verifying if the application was down while the server was shut and did it came up along with server boot.


Run script or command after server boot

In Suse Linux, you have to define commands or scripts in /etc/init.d/after.local to run them after server boots. I am running SLES 12 SP3 and my /etc/init.d/after.locallooks likes below –

root@kerneltalks # cat  /etc/init.d/after.local
#! /bin/sh
#
# Copyright (c) 2010 SuSE LINUX Products GmbH, Germany.  All rights reserved.
#
# Author: Werner Fink, 2010
#
# /etc/init.d/after.local
#
# script with local commands to be executed from init after all scripts
# of a runlevel have been executed.
#
# Here you should add things, that should happen directly after
# runlevel has been reached.
#

I added below command at end of this file.

echo "I love KernelTalks"

Then to test it, I rebooted the machine. After reboot, since command output is printed to console I need to check logs to confirm if the command executed successfully.

You can check logs of after local service as below :

# systemctl status after-local -l
● after-local.service - /etc/init.d/after.local Compatibility
   Loaded: loaded (/usr/lib/systemd/system/after-local.service; static; vendor preset: disabled)
   Active: active (exited) since Thu 2018-05-24 03:52:14 UTC; 7min ago
  Process: 2860 ExecStart=/etc/init.d/after.local (code=exited, status=0/SUCCESS)
 Main PID: 2860 (code=exited, status=0/SUCCESS)

May 24 03:52:14 kerneltalks systemd[1]: Started /etc/init.d/after.local Compatibility.
May 24 03:52:15 kerneltalks after.local[2860]: I love KernelTalks

If you observe the above output, the last line shows the output of our command which we configured in /etc/init.d/after.local! Alternatively, you can check syslog /var/log/messages file as well to check the same logs.

So it was a successful run.

Run script or command before server shutdown

To run a script or command before server initiate shutdown, you need to specify them in /etc/init.d/halt.local. Typical vanilla /etc/init.d/halt.local looks like below –

root@kerneltalks #  cat /etc/init.d/halt.local
#! /bin/sh
#
# Copyright (c) 2002 SuSE Linux AG Nuernberg, Germany.  All rights reserved.
#
# Author: Werner Fink, 1998
#         Burchard Steinbild, 1998
#
# /etc/init.d/halt.local
#
# script with local commands to be executed from init on system shutdown
#
# Here you should add things, that should happen directly before shuting
# down.
#

I added below command at end of this file.

echo "I love KernelTalks"

To make sure, this file is picked up for execution before the shutdown halt.local service should be running. Check if service is running and if not then start it.

# systemctl enable halt.local
halt.local.service is not a native service, redirecting to systemd-sysv-install
Executing /usr/lib/systemd/systemd-sysv-install enable halt.local
# systemctl start halt.local
# systemctl status halt.local
● halt.local.service
   Loaded: loaded (/etc/init.d/halt.local; bad; vendor preset: disabled)
   Active: active (exited) since Thu 2018-05-24 04:20:18 UTC; 11s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3074 ExecStart=/etc/init.d/halt.local start (code=exited, status=0/SUCCESS)

May 24 04:20:18 kerneltalks systemd[1]: Starting halt.local.service...

Then to test it, I shut down the machine. After boot, check logs to confirm if a command was run when the system was shut down.

# cat /var/log/messages |grep halt
2018-05-24T04:21:12.657033+00:00 kerneltalks systemd[1]: Starting halt.local.service...
2018-05-24T04:21:12.657066+00:00 kerneltalks halt.local[832]: I Love KernelTalks
2018-05-24T04:21:12.657080+00:00 kerneltalks systemd[1]: Started halt.local.service.

# systemctl status halt.local -l
● halt.local.service
   Loaded: loaded (/etc/init.d/halt.local; bad; vendor preset: disabled)
   Active: active (exited) since Thu 2018-05-24 04:21:12 UTC; 1min 18s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 832 ExecStart=/etc/init.d/halt.local start (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 512)

May 24 04:21:12 kerneltalks systemd[1]: Starting halt.local.service...
May 24 04:21:12 kerneltalks halt.local[832]: I Love KernelTalks
May 24 04:21:12 kerneltalks systemd[1]: Started halt.local.service.




That’s it. You can see our echo message is printed in logs which indicates commands successfully ran before shutdown.

In this way, you can configure your application start-stop commands in Suse Linux to start and stop application after boot and before the shutdown of the server. Also, you can schedule scripts to execute before shutdown and after boot of the Suse Linux server.

2 thoughts on “Execute command at shutdown and boot in Suse Linux

  1. Namita

    I followed this post to start and stop script during reboot. However, during shutdown also after.local file worked and during start up halt.local file worked. But this shouldn’t happen, please help

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.