Linux How-To

Execute command at shutdown and boot in Suse Linux

SEOClerks

Discover ways to setup instructions or scripts to execute at shutdown and boot in Suse Linux

Execute command at shutdown and boot in Suse LinuxExecute a command at shutdown and boot in Suse Linux

On this article, we’ll stroll you thru the process to schedule scripts at shutdown and boot in Suse Linux. Many occasions, we now have a requirement to start out sure software or service or script after server boots. Generally you need to cease software or service or run the script earlier than the server shut down. This may be executed robotically by defining instructions or scripts in sure information in Suse Linux.

Run script or command after server boot

In Suse Linux, you must outline instructions or scripts in /and so forth/init.d/after.native to run them after server boots. I’m operating SLES 12 SP3 and my /and so forth/init.d/after.locallooks likes beneath –

root@kerneltalks # cat /and so forth/init.d/after.native
#! /bin/sh
#
# Copyright (c) 2010 SuSE LINUX Merchandise GmbH, Germany. All rights reserved.
#
# Creator: Werner Fink, 2010
#
# /and so forth/init.d/after.native
#
# script with native instructions to be executed from init in any case scripts
# of a runlevel have been executed.
#
# Right here you need to add issues, that ought to occur instantly after
# runlevel has been reached.
#

1

2

three

four

5

6

7

eight

9

10

11

12

13

14

15

16

17

 

root@kerneltalks # cat  /and so forth/init.d/after.native

#! /bin/sh

#

# Copyright (c) 2010 SuSE LINUX Merchandise GmbH, Germany.  All rights reserved.

#

# Creator: Werner Fink, 2010

#

# /and so forth/init.d/after.native

#

# script with native instructions to be executed from init in any case scripts

# of a runlevel have been executed.

#

# Right here you need to add issues, that ought to occur instantly after

# runlevel has been reached.

#

 

I added beneath command at finish of this file.

echo “I really like KernelTalks”

 

echo “I really like KernelTalks”

 

Then to check it, I rebooted the machine. After reboot, since command output is printed to console I have to verify logs to substantiate if command executed efficiently.

You possibly can verify logs of after native service as beneath :

# systemctl standing after-local -l
● after-local.service – /and so forth/init.d/after.native Compatibility
Loaded: loaded (/usr/lib/systemd/system/after-local.service; static; vendor preset: disabled)
Lively: energetic (exited) since Thu 2018-05-24 03:52:14 UTC; 7min in the past
Course of: 2860 ExecStart=/and so forth/init.d/after.native (code=exited, standing=zero/SUCCESS)
Important PID: 2860 (code=exited, standing=zero/SUCCESS)

Could 24 03:52:14 kerneltalks systemd[1]: Began /and so forth/init.d/after.native Compatibility.
Could 24 03:52:15 kerneltalks after.native[2860]: I really like KernelTalks

 

# systemctl standing after-local -l

afternative.service /and so forth/init.d/after.native Compatibility

   Loaded: loaded (/usr/lib/systemd/system/afternative.service; static; vendor preset: disabled)

   Lively: energetic (exited) since Thu 20180524 03:52:14 UTC; 7min in the past

  Course of: 2860 ExecStart=/and so forth/init.d/after.native (code=exited, standing=zero/SUCCESS)

Important PID: 2860 (code=exited, standing=zero/SUCCESS)

 

Could 24 03:52:14 kerneltalks systemd[1]: Began /and so forth/init.d/after.native Compatibility.

Could 24 03:52:15 kerneltalks after.native[2860]: I love KernelTalks

 

In the event you observe above output, final line exhibits output of our command which we configured in /and so forth/init.d/after.native! Alternatively, you may verify syslog /var/log/messages file as nicely to verify similar logs.

So it was a profitable run.

Run script or command earlier than server shutdown

To run a script or command earlier than server provoke shutdown, it’s good to specify them in /and so forth/init.d/halt.native. Typical vanilla /and so forth/init.d/halt.native seems like beneath –

root@kerneltalks # cat /and so forth/init.d/halt.native
#! /bin/sh
#
# Copyright (c) 2002 SuSE Linux AG Nuernberg, Germany. All rights reserved.
#
# Creator: Werner Fink, 1998
# Burchard Steinbild, 1998
#
# /and so forth/init.d/halt.native
#
# script with native instructions to be executed from init on system shutdown
#
# Right here you need to add issues, that ought to occur instantly earlier than shuting
# down.
#

1

2

three

four

5

6

7

eight

9

10

11

12

13

14

15

16

17

 

root@kerneltalks #  cat /and so forth/init.d/halt.native

#! /bin/sh

#

# Copyright (c) 2002 SuSE Linux AG Nuernberg, Germany.  All rights reserved.

#

# Creator: Werner Fink, 1998

#         Burchard Steinbild, 1998

#

# /and so forth/init.d/halt.native

#

# script with native instructions to be executed from init on system shutdown

#

# Right here you need to add issues, that ought to occur instantly earlier than shuting

# down.

#

 

I added beneath command at finish of this file.

echo “I really like KernelTalks”

 

echo “I really like KernelTalks”

 

To verify, this file is picked up for execution earlier than shutdown halt.native service ought to be operating. Examine if service is operating and if not then begin it.

# systemctl allow halt.native
halt.native.service shouldn’t be a local service, redirecting to systemd-sysv-install
Executing /usr/lib/systemd/systemd-sysv-install allow halt.native
# systemctl begin halt.native
# systemctl standing halt.native
● halt.native.service
Loaded: loaded (/and so forth/init.d/halt.native; unhealthy; vendor preset: disabled)
Lively: energetic (exited) since Thu 2018-05-24 04:20:18 UTC; 11s in the past
Docs: man:systemd-sysv-generator(eight)
Course of: 3074 ExecStart=/and so forth/init.d/halt.native begin (code=exited, standing=zero/SUCCESS)

Could 24 04:20:18 kerneltalks systemd[1]: Beginning halt.native.service…

 

# systemctl allow halt.native

halt.native.service is not a native service, redirecting to systemdsysvset up

Executing /usr/lib/systemd/systemdsysvset up allow halt.native

# systemctl begin halt.native

# systemctl standing halt.native

halt.native.service

   Loaded: loaded (/and so forth/init.d/halt.native; unhealthy; vendor preset: disabled)

   Lively: energetic (exited) since Thu 20180524 04:20:18 UTC; 11s in the past

     Docs: man:systemdsysvgenerator(eight)

  Course of: 3074 ExecStart=/and so forth/init.d/halt.native begin (code=exited, standing=zero/SUCCESS)

 

Could 24 04:20:18 kerneltalks systemd[1]: Beginning halt.native.service...

 

Then to check it, I shutdown the machine. After boot, verify logs to substantiate if a command was ran when the system was shut down.

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

# systemctl standing halt.native -l
● halt.native.service
Loaded: loaded (/and so forth/init.d/halt.native; unhealthy; vendor preset: disabled)
Lively: energetic (exited) since Thu 2018-05-24 04:21:12 UTC; 1min 18s in the past
Docs: man:systemd-sysv-generator(eight)
Course of: 832 ExecStart=/and so forth/init.d/halt.native begin (code=exited, standing=zero/SUCCESS)
Duties: zero (restrict: 512)

Could 24 04:21:12 kerneltalks systemd[1]: Beginning halt.native.service…
Could 24 04:21:12 kerneltalks halt.native[832]: I Love KernelTalks
Could 24 04:21:12 kerneltalks systemd[1]: Began halt.native.service.

1

2

three

four

5

6

7

eight

9

10

11

12

13

14

15

16

17

18

 

# cat /var/log/messages |grep halt

20180524T04:21:12.657033+00:00 kerneltalks systemd[1]: Beginning halt.native.service...

20180524T04:21:12.657066+00:00 kerneltalks halt.native[832]: I Love KernelTalks

20180524T04:21:12.657080+00:00 kerneltalks systemd[1]: Began halt.native.service.

 

# systemctl standing halt.native -l

halt.native.service

   Loaded: loaded (/and so forth/init.d/halt.native; unhealthy; vendor preset: disabled)

   Lively: energetic (exited) since Thu 20180524 04:21:12 UTC; 1min 18s in the past

     Docs: man:systemdsysvgenerator(eight)

  Course of: 832 ExecStart=/and so forth/init.d/halt.native begin (code=exited, standing=zero/SUCCESS)

    Duties: zero (restrict: 512)

 

Could 24 04:21:12 kerneltalks systemd[1]: Beginning halt.native.service...

Could 24 04:21:12 kerneltalks halt.native[832]: I Love KernelTalks

Could 24 04:21:12 kerneltalks systemd[1]: Began halt.native.service.

 

That’s it. You possibly can see our echo message is printed in logs which signifies instructions efficiently ran earlier than shutdown.

On this manner, you may configure your software begin cease instructions in Suse Linux to start out and cease software after boot and earlier than shutdown of server. Additionally, you may schedule scripts to execute earlier than shutdown and after boot of Suse linux server.

Source link

Related Articles

Back to top button