To get the start time of a process on Solaris, you can use the ps command with the -o option to display the elapsed time since the process started, and then calculate the start time based on the current time. parapcc. com
#!/bin/bash
pid=$1
# Get the elapsed time in the format [[dd-]hh:]mm:ss
etime=$(ps -o etime= -p "$pid")
# Extract days, hours, minutes, and seconds from the elapsed time
days=0
hours=0
minutes=0
seconds=0
if [[ $etime == *-* ]]; then
days=$(echo $etime | awk -F'-' '{print $1}')
etime=$(echo $etime | awk -F'-' '{print $2}')
fi
hours=$(echo $etime | awk -F':' '{print $1}')
minutes=$(echo $etime | awk -F':' '{print $2}')
seconds=$(echo $etime | awk -F':' '{print $3}')
# Convert elapsed time to seconds
elapsed_seconds=$((days*86400 + hours*3600 + minutes*60 + seconds))
# Get the current time in seconds since the epoch
current_time=$(perl -e 'print time')
# Calculate the start time in seconds since the epoch
start_time=$((current_time - elapsed_seconds))
# Convert the start time to a human-readable format
start_time_formatted=$(perl -e 'print scalar localtime('$start_time')')
echo "$pid STARTED $start_time_formatted"
Here comes a fix.
The etime can be desribed by a regex.
#!/bin/bash
pid=$1
# Get the elapsed time in the format [[dd-]hh:]mm:ss
etime=$(ps -o etime= -p "$pid")
# Extract days, hours, minutes, and seconds from the elapsed time
[[ $etime =~ (([0-9]+)-)?(([0-9]+):)?([0-9]+):([0-9]+)$ ]]
typeset -i days=10#0${BASH_REMATCH[2]} hours=10#0${BASH_REMATCH[4]} minutes=10#0${BASH_REMATCH[5]} seconds=10#0${BASH_REMATCH[6]}
# Convert elapsed time to seconds
elapsed_seconds=$((days*86400 + hours*3600 + minutes*60 + seconds))
# Get the current time in seconds since the epoch
current_time=$(date +%s)
# Calculate the start time in seconds since the epoch
start_time=$((current_time - elapsed_seconds))
# Convert the start time to a human-readable format
start_time_formatted=$(perl -e 'print scalar localtime(shift)' "$start_time")
echo "$pid STARTED $start_time_formatted"
Thanks @MadeInGermany, you have won the solution for this round
I tried all and I believe the errors are as what you 've mentioned.
The date/time matches exactly what is the logon time on the database too.
$: ./show-ps-timestamp-00.bash 4164
-> MadeInGermany
4164 STARTED Fri Feb 28 02:46:21 2025
$: ./show-ps-timestamp-01.bash 4164
-> parapcc0
./show-ps-timestamp-01.bash: line 25: 08: value too great for base (error token is "08")
4164 STARTED Fri Mar 7 11:26:14 2025
$: ./show-ps-timestamp-02.bash 4164
-> Neo
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
4164 STARTED
SQL> select logon_time from v$session where paddr = ( select addr from v$process where spid = 4164 ) ;
LOGON_TIME
-------------------
2025-02-28 02:46:21
1 row selected.
This is a misfeature in bash. In a number context a number with a 0 prefix is considered octal.
Therefore the effort to prefix each value with a 10# so the number is always considered decimal (base 10), but then a missing value (the regex can give an empty value) gives error, so another 0 is prefixed.
You could do this later as well: