how to find out if application started for first time after boot
-
wrote on 11 May 2019, 12:00 last edited by
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???
-
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
-
wrote on 11 May 2019, 12:59 last edited by hskoglund 5 Nov 2019, 13:48
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...
-
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???
wrote on 11 May 2019, 16:07 last edited by Gerhard 5 Nov 2019, 18:44@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)
-
@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)
wrote on 14 May 2019, 10:41 last edited by@Gerhard that sounds a good approach.. but i am on a linux machine.. will try to figure out something alternative.
-
@Gerhard that sounds a good approach.. but i am on a linux machine.. will try to figure out something alternative.
wrote on 14 May 2019, 20:08 last edited by@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
-
@Gerhard that sounds a good approach.. but i am on a linux machine.. will try to figure out something alternative.
wrote on 14 May 2019, 20:44 last edited by JonB@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? -
@Gerhard that sounds a good approach.. but i am on a linux machine.. will try to figure out something alternative.
wrote on 15 May 2019, 06:34 last edited by@abdullahzubair109
You can also start the Background process once at System start. Therefore you can use a script in the etc/rc Directory.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?wrote on 15 May 2019, 07:41 last edited by@JonB it depends on distros..not all distros remove temp directory on boot
-
@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 ?
-
@JonB it depends on distros..not all distros remove temp directory on boot
wrote on 15 May 2019, 08:23 last edited byIf 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.
-
@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 ?
wrote on 16 May 2019, 19:33 last edited by@J.Hilk could be anything
1/12