Sponsored Content
Top Forums Shell Programming and Scripting Exit script when shutdown or reboot command is given Post 302880632 by bartgrefte on Saturday 21st of December 2013 01:32:58 PM
Old 12-21-2013
Exit script when shutdown or reboot command is given

This is probably a simple question, but I'm new with writing scripts for Linux (IPFire in this case) and Google wasn't helpful with this.

When creating a script, what is the best and/or proper way to have it exit automatically if the reboot or shutdown command is given? If that's even necessary, I don't know if Linux kills everything that's running by itself or waits like Windows for user input to kill something that was still running.

Anyway, the only thing I could come up with was
Code:
While [ "$(runlevel | sed 's/.* //')" = 3 ]

The idea is that the script stays in the while-loop while runlevel is 3, as in normal operation. When the shutdown or reboot command is given, runlevel changes to either 0 or 6, as in not 3, meaning the while-loop will exit. Well, that's the idea anyway.

Does anyone have any thoughts or comments on this?
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

using exit command in a shell script

Can it be done? If so, how? I would like a script to contain the exit command, and log me off at script completion. thanks (1 Reply)
Discussion started by: jpprial
1 Replies

2. UNIX for Dummies Questions & Answers

Shutdown and Reboot problem

Hi, i have sco openserver 5.0.6 but have a problem with shutdown or reboot commands. The string I use is "shutdown -g0 -y" but the system just hangs on "The sytem is down" . I used to get a "Safe to turn of" but now nothing. Same with "reboot" the sytem just freezes. Any suggestions? (1 Reply)
Discussion started by: VTechman
1 Replies

3. Shell Programming and Scripting

How to control/overide shutdown/reboot process?

Dear all, I want to control my server from shutting down and rebooting. It will ask for some question before the process, ex: who are you? what is the reason for shutting down? ... I think I can overide that binary of shutdown/reboot command, but it is confused. We have many way to... (5 Replies)
Discussion started by: fongthai
5 Replies

4. UNIX for Advanced & Expert Users

Emdedding my script in to exit command

Hi, How can I call my shell script, when I execute 'exit' command? For eg. I am logging out the root login by 'exit' command. My script 'colinfo' shold execute before logging out. I don't want to use any wrapper script. (2 Replies)
Discussion started by: sharif
2 Replies

5. UNIX for Advanced & Expert Users

shutdown and reboot unix server

Hi, please could someone advise the best command to shutdown and then for it to reboot back online again. Note: I shall be doing this from a telent session. regards venhart (3 Replies)
Discussion started by: venhart
3 Replies

6. UNIX for Advanced & Expert Users

Multiple ssh, shutdown and exit script

PROBLEM: I need to ssh into multiple remote machines, send a shutdown command and exit. The remote servers will then run their own scripts to gracefully shutdown their applications before shutting down. ONE: This is to be achieved without using public key authentication; this is being avoided... (2 Replies)
Discussion started by: ulemsee
2 Replies

7. UNIX for Dummies Questions & Answers

Script to force Oracle database shutdown when shutdown immediate does not work

I have Oracle 9i R2 on AIX 5.2. My Database is running in shared server mode (MTS). Sometimes when I shutdown the database it shutsdown cleanly in 4-5 mints and sometimes it takes good 15-20 minutes and then I get some ora-600 errors and only way to shutdown is by opening another session and... (7 Replies)
Discussion started by: aixhp
7 Replies

8. Linux

Reboot/Shutdown messages

Hi, I'm using 2.6.11 kernel on ARM 9. Below are the messages I get issuing different commands. When I give "reboot" I get run level 6 initiated and get the following messages. I have nothing to complaint about it. All the scripts in rc6.d are executed. The system is going down for reboot... (3 Replies)
Discussion started by: suryaemlinux
3 Replies

9. Shell Programming and Scripting

How to get the exit status of a command in nner script to the outer script?

Hi all, I have a shell script inside which i am executing another shell script. In the inner script im executing a command. i want the status of that command in the outer script to perform some validations. How to get its status please help!!1 Im using ksh. (2 Replies)
Discussion started by: Jayaraman
2 Replies

10. Shell Programming and Scripting

Issue with shutdown command in script (MacOS High Sierra)

Hello, I have a backup script that runs an rsync backup to an external drive. I use the script frequently on Windows and Linux and have installed it on a Mac. The script has an option to run shutdown after the backup has completed. Since backup can take hours to run, this is an option that is... (10 Replies)
Discussion started by: LMHmedchem
10 Replies
runlevel(8)						      System Manager's Manual						       runlevel(8)

NAME
runlevel - output previous and current runlevel SYNOPSIS
runlevel [OPTION]... [UTMP] DESCRIPTION
runlevel reads the system UTMP file, which defaults to /var/run/utmp when no alternate filename is given, to locate the most recent run- level record. The previous and current runlevel from that record are output separated by a single space. If there is no previous runlevel in the record, the letter N will be substituted. If no runlevel record can be found, runlevel outputs the word unknown and exits with an error. During system boot, the environment variables RUNLEVEL and PREVLEVEL will be set by the init(8) daemon, these come from the runlevel(7) event generated by telinit(8) or shutdown(8). When these environment variables are set, runlevel will output the values from these instead. Thus runlevel can be used in rc scripts as a replacement for the System-V who(1) -r command. OPTIONS
--quiet Does not output the current and previous runlevel, nor does it output unknown in the case of error (but it will exit with an error code). This may be used to test for the presence of a runlevel entry, or to check for errors reading from the file. EXIT STATUS
runlevel will exit with status 0 if a UTMP record was found, otherwise it will exit with status 1. ENVIRONMENT
RUNLEVEL runlevel will read the current runlevel from this environment variable if set in preference to reading from /var/run/utmp PREVLEVEL runlevel will read the previous runlevel from this environment variable if RUNLEVEL was given, in preference to reading from /var/run/utmp FILES
/var/run/utmp Where the current and previous runlevels will be read from. NOTES
The Upstart init(8) daemon does not keep track of runlevels itself, instead they are implemented entirely by its userspace tools. A change of runlevel is signalled by the runlevel(7) event, generated by either the telinit(8) or shutdown(8) tools. This event includes the new runlevel in the RUNLEVEL environment variable, as well as the previous runlevel (obtained from their own environment or from /var/run/utmp) in the PREVLEVEL variable. As well as generating the event, both tools write the new runlevel back to /var/run/utmp and append a new entry to /var/log/wtmp. AUTHOR
Written by Scott James Remnant <scott@netsplit.com> REPORTING BUGS
Report bugs at <https://launchpad.net/upstart/+bugs> COPYRIGHT
Copyright (C) 2009 Canonical Ltd. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICU- LAR PURPOSE. SEE ALSO
runlevel(7) init(8) telinit(8) shutdown(8) who(1) Upstart 2009-07-09 runlevel(8)
All times are GMT -4. The time now is 11:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy