1

This question already has an answer here:

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.

marked as duplicate by drewbenn, Stephen Rauch, G-Man, mdpc, countermode Dec 7 '17 at 8:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Is this helpful? – RubberStamp Dec 5 '17 at 3:20
  • 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 5 '17 at 3: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 5 '17 at 3: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 5 '17 at 4:48
  • I am not waiting for anything. Given this is not urgent, I will implement what's described in an answer. – Arcticooling Dec 5 '17 at 4:58
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.

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.

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