• Home
  • Disclaimer
  • Contact
  • Archives
  • About
  • Subscribe
  • Support
  • Advertise

Kernel Talks

Unix, Linux, & Cloud!

  • How-to guides
    • Howto
    • Disk management
    • Configurations
    • Troubleshooting
  • OS
    • HPUX
    • Linux
  • Miscellaneous
    • Software & Tools
    • Cloud Services
    • System services
    • Virtualization
  • Certification Preparations
    • AWS Certified Solutions Architect – Associate
    • AWS Certified Solutions Architect – Professional
    • AWS Certified SysOps Administrator – Associate
    • AWS Certified Cloud Practitioner
    • Certified Kubernetes Administrator
    • Hashicorp Certified Terraform Associate
    • Oracle Cloud Infrastructure Foundations 2020 – Associate
  • Tips & Tricks
  • Linux commands
You are here: Home / Howto

Execute command at shutdown and boot in Suse Linux

Published: May 24, 2018 | Modified: June 24, 2020



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.

⇠ Previous article
How to resolve setenv: command not found
Next article ⇢
How to install docker in Linux

Related stuff:

  • How to enter single user mode in SUSE 12 Linux?
  • File encryption / password protect file in HPUX
  • How to resolve the MFA entity already exists error
  • How to change timezone in Linux server (RedHat, CentOS, Ubuntu)
  • How to restart inetd service in Linux
  • How to remove password expiry in linux
  • How to safely remove disk from LVM
  • Howto get CPU details in HPUX
  • How to remount filesystem in the read-write mode under Linux
  • 4 steps guide for SMTP configuration in HPUX
  • How to disable GUI in SUSE Linux
  • Step by step procedure to take ignite tape backup in HPUX

Filed Under: Howto Tagged With: run command after server boot in suse, run script before shutdown, scheduling scripts at startup and shutdown

If you like my tutorials and if they helped you in any way, then

  • Consider buying me a cup of coffee via paypal!
  • Subscribe to our newsletter here!
  • Like KernelTalks Facebook page.
  • Follow us on Twitter.
  • Add our RSS feed to your feed reader.

Comments

  1. Pramod Mahor says

    May 26, 2018 at 2:24 pm

    good post
    thanks for sharing this post

    Reply
  2. Namita says

    August 24, 2018 at 3:37 pm

    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

Share Your Comments & Feedback: Cancel reply

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

Get fresh content from KernelTalks

  • Email
  • Facebook
  • RSS
  • Twitter

Get Linux & Unix stuff right into your mailbox. Subscribe now!

* indicates required

This work is licensed under a CC-BY-NC license · Privacy Policy
© Copyright 2016-2023 KernelTalks · All Rights Reserved.
The content is copyrighted to Shrikant Lavhate & can not be reproduced either online or offline without prior permission.