1

I currently have a script that I want to run

#! /bin/bash
Echo “plotting chart”
systemd-analyze plot > boot.svg
wait
Echo “rebooting”

shutdown -r +1
sleep 180

Is there a way to edit this so the script can restart the board 10 times and take a separate chart each time ?

New contributor
Squirrel is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • 1
    Sure. Have your script run on every boot, store state somewhere in the filesystem (e.g., a count of how many times you've booted), and read that in when your script runs and use that to decide what to do.
    – larsks
    Commented Jul 9 at 13:53
  • 1
    Dammit, this is a very thin line, and confusing. What's worse, I know I have myself often left comments very close to what @larsks wrote, so I am very far from blameless myself. I need to think about this some more because I am being inconsistent (I am writing this comment because I just caught myself about to give a partial answer in a comment to another question). I still think that partial answers in comments aren't helpful, but I should fix myself before attempting to fix others. So I have now deleted my original complaint, larsks, with my apologies.
    – terdon
    Commented Jul 10 at 14:02
  • @aviro, please see above.
    – terdon
    Commented Jul 10 at 14:02
  • Your script won't run. It looks like it should but you've got Echo instead of echo and you're using so-called smart quotes instead of straight double-quotes (or single-quotes) Commented Jul 10 at 16:48

3 Answers 3

2

@larsks already provided the definitive answer here as a comment - but just for the lafs....


#!/bin/bash

STATE="/tmp/$( basename "$BASHSOURCE" )"
REPEAT=10

function install() {
   echo "$REPEAT" > "$STATE" 
   echo "@reboot root $( realpath "$BASHSOURCE" )" >/etc/cron.d/rerunjob
} 
function uninstall() {
   rm -f /etc/cron.d/rerunjob
   rm -f "$STATE"
}

if [ "install" = "$1" ]; then
   install
   reboot
   exit
fi

read REPEAT <"$STATE"
REPEAT=$(( REPEAT - 1 ))

if [ 0 -lt "$REPEAT" ]; then
    sleep 5
    systemd-analyze plot > "/root/boot-$REPEAT.svg"
    echo "$REPEAT" > "$STATE" 
    reboot
else
    uninstall
fi    

(What was it Einstein said about repeating things?)

1
  • "larsks already provided the definitive answer here as a comment" - not really. They made a comment. You have provided an answer Commented Jul 10 at 21:49
1

I don't think it'll work for your particular case but the approach can be used in similar situations and it's too long for a comment

On Linux it could be achieved by enabling hibernation and doing echo reboot | sudo tee /sys/power/disk and hibernating the system. When the system comes back up from hibernation the script would carry on.

However, looking at your question I guess that you want to specifically gather boot statistics so you have to restart init (systemd).

0

First at all, you need some file to remember the reboot times:

$ echo 1 > xx

$ read x 
9       ## key in by keyboard

$ echo ${x}
9       ## Check the value of {x} 

$ read x  < xx
##  Read the value from file `xx` 

$ echo ${x}
1       ## Check the value of {x}, make sure it had been changed! 

Then, You have to let the value plus one:

for bash/ksh :

$ x=4

$ echo ${x}
4

$ x=`expr ${x} + 1`

$ echo ${x}
5

for tcsh/csh, you might need to change it into :

$ set x=4 

Now, write this number into your record file :

$ echo ${x} > xx  

Then, after reboot, read the value from the record file, +1, then write it back and you can use this number as your log file, like:

echo Hell~o Weird  > boot_${x}.svg 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .