3

I have a bash script that changes some configuration in OS and database and it needs reboot then other commands are needed to be run. something like this:

 newvalue1=
 ip=
 .
 .
 some commands
 .
 .
 reboot
 .
 .
 some other commands

what to do to make this happen?

(there are 2 other questions like this on stack exchange, Q1 and Q2 but they were asked long time ago and users are no longer active to ask them questions. i asked again to find out if there is new ideas and answers)

BlackCrystal
  • 716
  • 14
  • 39
  • Which distribution, version, etc.? – muru Jun 12 '19 at 11:58
  • @muru Centos 6.7 and 6.9 and 7.6 – BlackCrystal Jun 12 '19 at 12:01
  • CentOS 6 released 3 years before Ubuntu 14.04 did. Not a release for new ideas. CentOS 7, perhaps. Systemd might have something new. Better add that to the question. – muru Jun 12 '19 at 12:04
  • Might be relevant: https://unix.stackexchange.com/questions/233300/running-a-systemd-service-unit-at-next-boot-but-not-subsequent-boots/268268 – muru Jun 12 '19 at 12:07

1 Answers1

2

If you can modify the script and if you can use crontab, then you might use the @reboot timing in crontab to rerun the script. The behaviour of the script would be dual, depending on how it's called. So eg. if it's run with a parameter -s or --second-run, then it would do the after-reboot stuff. Introducing two main functions in the script like first_run and second_run would help organise it. Here's a sketch of the script:

  1. Check how it's called (with or without the -s parameter).
  2. If without -s:

    a) Do the initial stuff.

    b) Add the second run with @reboot to crontab.

    c) Reboot.

  3. If with -s:

    a) Do the after-reboot stuff.

    b) Remove the rerun from crontab.