1

I want that for a given number of system boots (say, 1 or 2 system boots), a message will appear.

I will store the message somewhere, maybe in a variable:

message='If you finished testing, make sure to remove the testing application.'!'

Notes:

  • I use Ubuntu server 16.04 and aim for a solution for GUI-less systems.

  • The purpose of the code is to have a nice way to remind myself of a certain task, if, for some reason, I didn't recall that it is a good time to do it.

  • I assume I should store the variable somewhere "deeper" than RAM, maybe in a file I'll create for it, maybe another method. I'm not sure.

  • Cron isn't good because it's repetitive, and not for a given number of executions.

  • This is good in case you reboot / turn off / power outage --- you get a reminder for an important task.

Clarification

I tried to describe a case when a user turnes on the machine, and in the moment the primary prompt appears, along with it, will appear the message I mentioned.

  • If I understood correct, this will keep happening as long is the command is in cron, but I want it to run a well defined number of times, not as long as it is in cron. – Arcticooling Dec 05 '17 at 03:32
  • 1
    You could use a cron script to update a counter in a file every boot or some event or some timeframe... or you could use a systemd service in like manner... those are the first things that came to my mind. – RubberStamp Dec 05 '17 at 03:38
  • I am not sure what you are asking for other than the tools and suggestions you already have from @RubberStamp. Are you waiting for an implementation? – bu5hman Dec 05 '17 at 04:48
  • I am not waiting for anything. Given this is not urgent, I will implement what's described in an answer. – Arcticooling Dec 05 '17 at 04:58
  • I've totally edited both the headline and question. – Arcticooling Dec 05 '17 at 05:02

2 Answers2

2

IMHO no purpose doing it "on boot" if there is noone to see it, so it is best made part of your login... You can call a script from your ~/.profile. The script can check if 1) the test application is still there and 2) some delay has passed since it last warned you (for instance by checking the time stamp of some file in in /var/tmp) in which case it warns you again (and touch'es the marker file).

With such a "don't pester me too often" script you can even run it each time bash returns to the command prompt by putting it in bash's PROMPT_COMMAND variable.

xenoid
  • 8,888
1

Depending on your system, you may be able to use cron's @reboot

However, a systemd service file is probably a more stable method.

So:

As hinted in the comments: This service file ... or something like it... and...

This script:

#!/bin/bash
# Set _endcount to number of times you want to count minus one.
_endcount=2

if [ ! -f count.txt ]; then
   echo "1" > count.txt
   echo -e "No counter\nInitializing Counter"
   exit;
else 
   mycount=$( cat count.txt )
   if [ $mycount = $_endcount ]; then
      echo -e "If you finished testing, make sure to remove the testing application!"
      echo "1" > count.txt
   fi
   echo "Warning $mycount, Expires at $_endcount"
   count=$(($mycount + 1))
   echo "$count" > count.txt
fi

Or something like it. There are probably 15 ways to solve this problem and 100 ways to write this script.

RubberStamp
  • 7,318