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:

 some commands
 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)

  • 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: unix.stackexchange.com/questions/233300/… – muru Jun 12 '19 at 12:07

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.

| improve this answer | |
  • would you pleade add a proper example ? – BlackCrystal Jun 12 '19 at 13:10
  • 2
    @BlackCrystal It's quite complex, so rather not. My answer is supposed to be a sketch. I have no CentOS also, so could not test anythow. – user147505 Jun 12 '19 at 13:13
  • i wrote a script inspired by your answer, those 2 question and @muru comments and it worked. finally! – BlackCrystal Jun 17 '19 at 10:49

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.