I'm writting a Perl script for which I need the uptime in seconds to do some calculation, in all the machine in the shop (i.e. linux, SunOS, and AIX). I have a way to get the uptime for linux (/proc/uptime), and SunOS (kstat -p unix:0:system_misc:boot_time), thanks to an another posting on this site, but I can find a good way of getting it for AIX. I don't really like the idea of parsing uptime with reg-ex, since uptime changes when the machine is been up just sec, mins, days, or over a year.
3 Answers
This snippet in C works under AIX 6.1. I can't give you the source article as I only have source code left.
#include <utmpx.h>
int main ( )
{
int nBootTime = 0;
int nCurrentTime = time ( NULL );
struct utmpx * ent;
while ( ( ent = getutxent ( ) ) ) {
if ( !strcmp ( "system boot", ent->ut_line ) ) {
nBootTime = ent->ut_tv.tv_sec;
}
}
printf ( "System was booted %d seconds ago\n", nCurrentTime - nBootTime );
}

- 2,812
- 23
- 27
Parse the output of last(1)
?
Find a file/directory that is only created/refreshed at boot time and stat it?
Frankly, using different regexs to handle the different possible outputs from uptime
doesn't sound so bad.

- 117,087
- 18
- 149
- 283
Answering old thread for new interested stumblers.
We're going to make a lightweight C program called getProcStartTime
that you'll have plenty of other uses for. It tells you when a process was started, given the PID. I believe you will still get a time stamp down to the second even if the process was started months or years ago. Save this source code as a file called getProcStartTime.c:
#include <time.h>
#include <procinfo.h>
main(argc, argv)
char *argv[];
{
struct procentry64 psinfo;
pid_t pid;
if (argc > 1) {
pid = atoi(argv[1]);
}
else {
printf("Usage : getProcStartTime pid\n");
return 1;
}
if (getprocs64(&psinfo, sizeof(struct procentry64), NULL, sizeof(struct fdsinfo64) , &pid, 1) > 0) {
time_t result;
result = psinfo.pi_start;
struct tm* brokentime = localtime(&result);
printf("%s", asctime(brokentime));
return 0;
} else {
perror("getproc64");
return 1;
}
}
Then compile it:
gcc getProcStartTime.c -o getProcStartTime
Here's the magic logic: AIX just like Linux has a process called init
with PID 1. It can't be killed or restarted. So the start time of PID 1 is the boot time of your server.
./getProcStartTime 1
On my server, returns Wed Apr 23 10:33:30 2014; yours will be different.
Note, I originally made getProcStartTime
specifically for this purpose, but now I use it in all kinds of other scripts. Want to know how long an Oracle database has been up? Find the PID of Oracle's PMON and pass that PID as your arg after getProcStartTime
.
If you really want the output as an integer number of seconds, it would be an easy programming exercise to modify the code above. The name getProcUptime
is just a suggestion. Then you could just call:
./getProcUptime 1
UPDATE: Source code and precompiled binary for AIX 6.1/7.1 have been put on my Github repo here: https://github.com/Josholith/getProcStartTime

- 3,443
- 20
- 27
-
Hi Joshua, in my current AIX environment I haven't gcc compiler. Can I use cpp to compile your code? Do you have a compiled version of it? Thank you very much in advance. – Fabrizio A Oct 30 '17 at 12:02
-
@FabrizioA, give it a try with `cpp`. The code is just plain C, so I don't see any reason it should not work. In case it doesn't work, I just uploaded it to my github repo, including both the source and the binary compiled on AIX 6.1 (but also works on 7.1). https://github.com/Josholith/getProcStartTime – Joshua Huber Oct 30 '17 at 15:05
-
Thank you very much @Joshua Huber for your script. However I have a problem with it, if I execute ./getProcStartTime 1 (I need system uptime) I get the error "getproc64: Invalid argument" ... any suggestion? My version of AIX is 5.3.0.0 – Fabrizio A Oct 30 '17 at 16:38
-
@FabrizioA, was the error you got from my precompiled binary, or did you get it to compile? I no longer have any AIX systems to play with. I used to work on AIX 5.3, but by the time I wrote this utility, I was on all AIX 6.1/7.1 servers. I'm thinking between versions, they may have changed a data structure. I would recommend to compare `/usr/include/procinfo.h` between a 5.3 and 6.1 server. This header file gets referenced when you compile the program. – Joshua Huber Oct 30 '17 at 16:59
-
@FabrizioA, this may help: "gcc/g++ Bundles for AIX 5.2 and 5.3" ftp://ftp.hvcc.edu/pub/pware/aix53/bundles/gcc-4.2.2/gccg++.pware-bundle-AIX53.tar.gz (found on site https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Power+Systems/page/AIX+and+Open+Source) – Joshua Huber Oct 30 '17 at 17:06
-
I can't access on your first ftp link... I have discovered it either on IBM website... but I can't download it... :'( – Fabrizio A Oct 30 '17 at 17:27