I have a program that has ill effects if run from .bashrc and I've dug deeper and found a minimal reproducible example.

The code below will print the elapsed time every second. If I run this program any time well after boot it outputs sequential seconds, as expected. If I run this program immediately after boot, manually or with .bashrc, the seconds in the output will skip at some point; from .bashrc at 24 they skip to 37 (then continue on as expected 38, 39...), if I run it manually from a shell it happens at some earlier time, depending on how long after boot I run it.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (int argc, char *argv[])
    int start_time = time(NULL);
    int diff = 0;
    int prev = 0;

    while (1)
        diff = difftime(time(NULL), start_time);
        if (diff != prev)
            printf("Time: %d\n", diff);
        prev = diff;

What could cause this?

  • 2
    Anything run from .bashrc is going to be a disaster because it is not intended to run code – Milliways Jan 8 at 7:35
  • Why? What alternative do you suggest? Also, my problem occurs even if run manually shortly after boot. – user120300 Jan 8 at 7:37
  • No one can suggest an "alternative" because it is pointless. – Milliways Jan 8 at 7:39
  • How do you then automatically run a program at start? – user120300 Jan 8 at 7:40
  • 1
    .bashrc has nothing to do with "at start" .... .bashrc is what is done on user login – Jaromanda X Jan 8 at 7:43

First of all: I also suggest together with all others from the comments, don't use ~/.bashrc to start a still running program. The best way is to create a service with a systemd Unit file.

The problem with your program is that the Raspberry Pi does not have a real time clock. It needs an internet connection and some time after boot up to initialize the network and to synchronize the local time with a time server on the internet. That is exactly the point where you see the "jump" in the time and continuing as expected. To solve your problem just take into account the time until synchronization with a time server has finished. Have a look at How to know if time is synced?.

  • Thank you. Is there some way to know when this synchronization is complete? As it stands this is part of a larger problem I am having and I thought this might be another indicator. Is there some way to see if all boot processes are complete? – user120300 Jan 8 at 11:12
  • @user120300 I have updated the answer at the end. If you use systemd you have many options to check dependencies to other services. Depending on the user interface a boot up process is usually finished After=multi-user.target or After=graphical.target (see man systemd.unit). – Ingo Jan 8 at 11:19
  • @user120300: This recent Q&A may help. – Seamus Jan 8 at 19:43
  • @user120300 If this answer your question, please mark the answer as the accepted one with a click on the tick on its left side. That prevents your Question from being shown as an unsolved Post to the community and saves them/us a lot of work. – Ingo Jan 9 at 9:20

As an alternative to @Ingo's excellent answer; i.e. "do not try to start a progrm in ~/.bashrc - create a systemd Unit file instead.":

Do not try to start a progrm in ~/.bashrc - create a cron job instead.

Complete instructions for setting up a cron job to start your program each time the RPi boots may be found here.

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.