2

I am wanting to run multiple scripts after boot up. When the machine boots up, one script would run and force a reboot. Then, after the reboot, another script would run and then reboot. Need this to happen about four times. Is this possible?

3
  • Yes it is possible. You could save the current status of the system into a log file. Then a master script could read the last written status and run conditionally a certain script of the bundle. – pa4080 Aug 6 '20 at 15:58
  • I like that answer a lot. That's an interesting way to go about this. So, I'm thinking I could then just take all four scripts and combine into one with a conditional response for each set of commands. how would I create the log file so that it changes after each reboot and then how would I refer to the log file? Sorry. Really trying to wrap my head around the idea of automation using bash scripts. I really appreciate the direction – noobuntu Aug 6 '20 at 17:53
  • I've converted my comment into an answer with example. – pa4080 Aug 6 '20 at 19:51
1

Yes it is possible. You could save the current status of the system into a log file. Then a master script could read the last written status and run conditionally a certain script or function. Here is an example of such script:

$ cat ~/status-reboot.sh
#!/bin/bash

STATUS_LOG="$HOME/our.status.log"

# Determinate whether the log file exists ? get the status : set status0
if [[ -f $STATUS_LOG ]]
then
        CURRENT_STATUS="$(cat "$STATUS_LOG")"
else
        CURRENT_STATUS="stage0"
        echo "$CURRENT_STATUS : $(date)"
        echo "$CURRENT_STATUS" > "$STATUS_LOG"
        # You could reboot at this point,
        # but probably you want to do action_1 first
fi

# Define your actions as functions
action_1()
{
        # do the 1st action

        CURRENT_STATUS="stage1"
        echo "$CURRENT_STATUS : $(date)"
        echo "$CURRENT_STATUS" > "$STATUS_LOG"
        exit # You could reboot at this point
}

action_2()
{
        # do the 2nd action

        CURRENT_STATUS="stage2"
        echo "$CURRENT_STATUS : $(date)"
        echo "$CURRENT_STATUS" > "$STATUS_LOG"
        exit # You could reboot at this point
}

case "$CURRENT_STATUS" in
stage0)
  action_1
  ;;
stage1)
  action_2
  ;;
stage2)
  echo "The script '$0' is finished."
  ;;
*)
  echo "Something went wrong!"
  ;;
esac

Here is how it works within the command line:

$ ./status-reboot.sh
stage0 : Thu Aug  6 22:45:29 EEST 2020
stage1 : Thu Aug  6 22:45:29 EEST 2020

$ ./status-reboot.sh
stage2 : Thu Aug  6 22:45:33 EEST 2020

$ ./status-reboot.sh
The script './status-reboot.sh' is finished.

$ ./status-reboot.sh
The script './status-reboot.sh' is finished.

I think it should work without problem with crontab entry as this:

@reboot sleep 15 && "$HOME/status-reboot.sh" >> "$HOME/our.progress.log"
  • Please use full paths to the commands into your scripts used with crontab.

Reference: tldp.org - Using case statements

2
  • Thanks a lot @pa4080. I'll try this out. i really appreciate the time you put into this explanation. – noobuntu Aug 7 '20 at 13:13
  • You are welcome, @noobuntu :) – pa4080 Aug 7 '20 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.