Exit script when shutdown or reboot command is given


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit script when shutdown or reboot command is given
# 1  
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?
# 2  
Old 12-21-2013
The OS will terminate the processes, no need to bother.
# 3  
Old 12-23-2013
The OS will terminate the process but if you wanna be clean about this you should handle your application through services management.

- Start script
- Stop script
- Add service

That way when the system boots it will check all the services that need to be started and start your application automaticly. Same thing while your system shuts down it will check all the services to end and will terminate your application cleanly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question