LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Execute a command , reboot and then execute another command (http://www.linuxquestions.org/questions/linux-newbie-8/execute-a-command-reboot-and-then-execute-another-command-687393/)

ganeshp@moris.org 12-01-2008 03:21 AM

Execute a command , reboot and then execute another command
 
Hi All,

Is there a way to execute some command and then after the command completes automatically reboot the system and then after the system reboots execute another command ?

For example look at the sequence shown below
(1) Execute command-1
(2) After the command-1 in (1) is completed,reboot the system
(3) Execute command-2
(4) After execution of command-2 reboot the sytem

Is there a way i can automate this process so that i need not reboot the system manually

Thanks and Regards
Ganesh

Tinkster 12-01-2008 03:31 AM

Hi,

Welcome to LQ!

You could write a "self-modifying script" that you call from
rc.local (or whichever the equivalent in your distro is - you
didn't put it in your profile).

Something along the line of

do something && sed -i '1d' my_script && reboot
do something_else && sed -i '1d' my_script && reboot



You get the idea.... untested, of course, since I
have no intention of knocking my machine over the head. :)


Cheers,
Tink

RMLinux 12-01-2008 04:16 AM

Quote:

Originally Posted by ganeshp@moris.org (Post 3360165)
Hi All,

Is there a way to execute some command and then after the command completes automatically reboot the system and then after the system reboots execute another command ?

For example look at the sequence shown below
(1) Execute command-1
(2) After the command-1 in (1) is completed,reboot the system
(3) Execute command-2
(4) After execution of command-2 reboot the sytem

Is there a way i can automate this process so that i need not reboot the system manually

Thanks and Regards
Ganesh

1. you write a script: shutdown -r now.
2. command1 here.
3. shutdown -r now
4. command2 here.

5. make it executable chmod 777 [name of file].
6. link it in rc3.d, rc4.d, rc5.d by typing

ln -s [your script] /etc/rc3.d/S97myscript
ln -s [your script] /etc/rc4.d/S97myscript
ln -s [your script] /etc/rc5.d/S97myscript

S of 97 meaning "Start" ...K is "Kill"...make sure you choose different number after Snn.

7. if you finished... delete the link by rm command.

ganeshp@moris.org 12-03-2008 01:51 AM

Hi All,

Thank you very much for your kind support. The Linux distribution i am using is SLES 10 SP1.
I would like to list some of the things that i found out in the course of solving this issue.

boot.local runs before the runlevels are entered
after.local runs after entering the final run level
halt.local runs on the system halt

To solve the problem in my hand i created a file called "to_do.sh" and then executed this file from file "after.local".
File "after.local" was not present in /etc/rc.d/ folder. I created this file and added the line "sh /tmp/to_do.sh" in this file.

Content of my "to_do.sh" is given below.

With this i was able to achieve my requirement.

Thank you,
Regards,
Ganesh

Content of my "to_do.sh"In this file i am reading the choice after reboot from a file called choice.txt and after the current choice is executed i am rewriting my next choice to choice.txt for the next reboot.

#!/bin/sh

current_choice=$(</tmp/choice.txt)

if [ $current_choice -eq 1 ]
then
#Step-1 Execute my command-1, write the command to execute below

#Step-2 Change the choice ,so that in next reboot the elif gets executed
echo "2">/tmp/choice.txt

#Step-3 reboot
reboot

elif [ $current_choice -eq 2 ]
then
#Step-1 Execute my command-2, write the command to execute below

#Step-2 Reboot and run
echo "3">/tmp/choice.txt

#Step-3 reboot
reboot

else
echo "Completed">/tmp/Comp.txt
fi


All times are GMT -5. The time now is 05:28 AM.