Unsolved how to find out if application started for first time after boot
-
i have a text editor.. every time i start this after boot, i want to run some underlying background process .. for that i must know if the text editor started for first time after boot.. how can i implement such feature???
-
Not directly your use case, but QtSingleApplication might give you some insight how to do it.
Regards
-
If you're on Windows, you could use the ::GetTickCount64(), it returns # of milliseconds since boot.
Use this number to calculate the date and time for the most recent boot, like this:
QDateTime dtBoot = QDateTime::currentDateTime().addMSecs(0 - ::GetTickCount64());
(need to #include "windows.h" )
Then save this date/time in a disk file, for example an .ini file, when you start the text editor. Then before you save, you check if the previous, saved boot time is the same as the current. If not, the text editor has started for the first time since boot.
Edit: I tested my idea, spotted one problem: the exact number of milliseconds varies due to rounding in ::GetTickCount64(), if I run my test program with a couple of minutes apart each time, it will say the boot time of my computer is:
2019-05-11 06:35:54.786
2019-05-11 06:35:54.776
2019-05-11 06:35:54.783
2019-05-11 06:35:54.780
2019-05-11 06:35:54.784So some form of rounding to the nearest second needs to be applied...
-
@abdullahzubair109
Try it with a "Volatile Registry Key". These entries are not persistent and would be filled at System start. If your app creates this entry than you can check if this entry is present, if not then the app was not started yet.Gerhard
(sorry for my english)
-
@Gerhard that sounds a good approach.. but i am on a linux machine.. will try to figure out something alternative.
-
@abdullahzubair109
create a Semaphore at first start of the process, in your app check if Semaphore is created or locked. Not so familar with the new POSIX semaphores, look at the man pages or Google Linux IPC (inter process communication).Gerhard
-
@abdullahzubair109
For quick & dirty (doubtless @Gerhard's semaphore is better), can't you just create a file in/tmp
and check for its existence? Doesn't your/tmp
get cleared out on reboot? -
@abdullahzubair109
You can also start the Background process once at System start. Therefore you can use a script in the etc/rc Directory.Gerhard
-
@JonB it depends on distros..not all distros remove temp directory on boot
-
@abdullahzubair109
how much influence do you have about the Linux System.Is it a custom one or could it be anything, depending on the user of your program ?
-
If they are configured correctly, all modern linux distros should clear the /tmp directory on reboot. In fact, most of them mount /tmp as a RAM filesystem, so it is volatile anyways. The stub file under /tmp or /var/tmp or /var/run is the historical way in UNIX to check whether something is already running.
-
@J.Hilk could be anything