Running time in secs for a process in linux - linux

I am trying to find the running time of a process on a linux server. I using etime to achieve this. Here's the code i am using
ps -eo etime,args | grep "process"|awk '{print $1}'
The sample output for this
28-08:42:13
I am then doing the following to convert this to seconds and sending myself a mail when the runtime is less that 30 mins(1800 secs)
ps -eo etime,args | grep "process"|awk '{print $1}'|awk -F'[-: ]+' '/:/ {t=$2+60*$1; print t}'
The problem i am seeing is, when converting to seconds, it's taking the $2(secs) and $1(mins) from the running time and sending me a mail even though the process has been up for 28-08:42:13.
Is there a better way of doing the conversion of runtime to secs? Using $3 and $4 from the first output doesn't work for cases where the runtime is less than 1 hour. It's picking a garbage value and returning a different value than the actual running time.
Please help.

etime is human readable elapsed time. etimes is machine readable elapsed time (in seconds):
$ ps -o etimes,etime,cmd $$
ELAPSED ELAPSED CMD
515615 5-23:13:35 bash

I am using this and it works.
ps -eo etime,args | grep "process"|awk '{print $1}'|awk -F'[-: ]+' '/:/ && NF==5 {t=$5+60*($4+60*($3+24*($2+365*$1))); print t} /:/ && NF==4 {t=$4+60*($3+60*($2+24*$1)); print t} /:/ && NF==3 {t=$3+60*($2+60*$1); print t} /:/ && NF==2 {t=$2+60*$1; print t}'

Related

Getting process from yesterday

I want to obtain all the process that are running in the system, but only from yesterday.
I am using this, ps -eo etime,pid
but i need only list the process from yesterday, any idea?
INFO: active from yesterday, process actually running from yesterday
Thanks in advance
If you want all the pids that have been running for more than 1 day but less than 2:
ps -e -o pid= -o etime= | sed 's/^ *//' | awk -F '[ -]+' 'NF>2 && $2==1 {print $1}'
if you want just more than 1 day, change it to $2>=1
It is not clear what exactly you need
But if you needed it from a specific time, just add it to crontab.
For example:
0 2 * * * ps -eo etime,pid>/tmp/processes.txt
This way you will have a snapshot from the processes running on 2:00
If you didnt mean that please be more specific of what exactly do you need

Net Usage (%) of a Process in Linux

I'm trying to build a script in Linux (Debian 10) that shows the net usage (%) of a process passed as an argument.
This is the code, but there isn't any output:
ProcessName=$1
(nethogs -t ens33 | awk '/$ProcessName/{print $3}') &> output.txt
While using tracemode nethogs -t, first field of output is program and it can consists of irregular number of arguments.
In case of brave:
/usr/lib/brave-bin/brave --type=utility --utility-sub-type=network.mojom.NetworkService --field-trial-handle=18208005703828410459,4915436466583499460,131072 --enable-features=AutoupgradeMixedContent,DnsOverHttps,LegacyTLSEnforced,PasswordImport,PrefetchPrivacyChanges,ReducedReferrerGranularity,SafetyTip,WebUIDarkMode --disable-features=AutofillEnableAccountWalletStorage,AutofillServerCommunication,DirectSockets,EnableProfilePickerOnStartup,IdleDetection,LangClientHintHeader,NetworkTimeServiceQuerying,NotificationTriggers,SafeBrowsingEnhancedProtection,SafeBrowsingEnhancedProtectionMessageInInterstitials,SharingQRCodeGenerator,SignedExchangePrefetchCacheForNavigations,SignedExchangeSubresourcePrefetch,SubresourceWebBundles,TabHoverCards,TextFragmentAnchor,WebOTP --lang=en-US --service-sandbox-type=none --shared-files=v8_context_snapshot_data:100/930/1000 0.0554687 0.0554687
so $3 will no longer be as expected, you need to get last column of output using $(NF) as follow:
... | awk /$ProcessName/'{print $(NF)}'
for second last column:
... | awk /$ProcessName/'{print $(NF - 1)}'
What I'm doing wrong?
What you're doing wrong is single-quoting $ProcessName while wanting this to be parameter-expanded. To get the expansion, just don't quote there, e. g.:
… awk /$ProcessName/'{print $3}' …

Unix shell command for uptime monitoring

apologies, I'm not a Unix guy, Windows and Powershell is more my area. I need to check the uptime on Linux servers using a shell command that can be invoked from SCOM.
I have been able to get the uptime in seconds back into SCOM using...
cat /proc/uptime | gawk -f ' ' '{print $1}'
However, SCOM does not pick this up as numerical, I think it's treating the returned output as a string.
I'd like a shell command that returns a 0 or 1 if the number of seconds is less than one day (86400).
I've been experimenting with [test -gt] but can't seem to get it working?
thanks
Does this work for you?
cat /proc/uptime | gawk '{print ($1>86400)?1:0;}'

Filter lines by number of fields

I am filtering very long text files in Linux (usually > 1GB) to get only those lines I am interested in. I use with this command:
cat ./my/file.txt | LC_ALL=C fgrep -f ./my/patterns.txt | $decoder > ./path/to/result.txt
$decoder is the path to a program I was given to decode these files. The problem now is that it only accept lines with 7 fields, this is, 7 strings separated by spaces (e.g. "11 22 33 44 55 66 77"). Whenever a string with more or less fields is passed into this program makes it crash, and I get a broken pipe error message.
To fix it, I wrote a super simple script in Bash:
while read line ; do
if [[ $( echo $line | awk '{ print NF }') == 7 ]]; then
echo $line;
fi;
done
But the problem is that now it take ages to finish. Before it took seconds and now it takes ~30 minutes.
Does anyone know a better/faster way to do this? Thank you in advance.
Well perhaps you can insert awk between instead. No need to rely on Bash:
LC_ALL=C fgrep -f ./my/patterns.txt ./my/file.txt | awk 'NF == 7' | "$decoder" > ./path/to/result.txt
Perhaps awk can be the starter. Performance may be better that way:
awk 'NF == 7' ./my/file.txt | LC_ALL=C fgrep -f ./my/patterns.txt | "$decoder" > ./path/to/result.txt
You can merge fgrep and awk as a single awk command however I'm not sure if that would affect anything that require LC_ALL=C and that it would give better performance.

Perl script log to file, output lag

I have a Perlscript which does some logfile parsing and sometimes executes a bash command:
$messagePath = `ls -t -d -1 $dir | head -n 5 | xargs grep -l "$messageSearchString"\`;
I start my perl script like this ./perlscript.pl > logfile.log.
Now I do a tail on the logfile to watch the progress, but the output gets stuck every time at the line I described above.
The output will stop there for some seconds and then continue. ???
To profile the problem I wrapped it like this:
print `date`;
$messagePath = `ls -t -d -1 $dir | head -n 5 | xargs grep -l "$messageSearchString"`;
print `date`;
The output shows that the command does not consume a lot of time:
So 6. Okt 22:35:04 CEST 2013
So 6. Okt 22:35:04 CEST 2013
If I run the script without redirecting the output to a file there is no LAG.
Any idea why?
I haven't tried to duplicate your behaviour, but it might be a stdout buffering problem. Try with:
$| = 1;
$messagePath = `ls -t -d -1 $dir | head -n 5 | xargs grep -l "$messageSearchString"`;
Update
I have tried to duplicate the behaviour you observe: I've had to make some assumptions but I believe my suspicion was correct. Here I'm piping, but it's the same as redirecting to a file and tailing that file:
./test.pl | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }'
Without $| = 1, output is buffered and aggregated:
2013-10-06 23:08:27 Saluton, mondo: /home/lserni/test.sh
2013-10-06 23:08:27
2013-10-06 23:08:27 Waiting 10s...
2013-10-06 23:08:27 Saluton denove!
With the modification, each line is printed as it is generated:
2013-10-06 23:09:09 Saluton, mondo: /home/lserni/test.sh
2013-10-06 23:09:09
2013-10-06 23:09:09 Waiting 10s...
2013-10-06 23:09:19 Saluton denove!
I expect that your script is doing something that takes some seconds, and which is not generating that messagePath; and the output will be delayed until Perl has a sizeable chunk of data to send along, giving the impression that it's that line that's stalling.
I forgot: the timing pipe comes from here.
In situations like yours, I've had some success using the unbuffer command. It runs a command in an environment that looks to the command like it's outputting to a tty so it doesn't buffer its output. I don't know how to apply it exactly in your case, so if you want to try it, you will have to experiment a little.

Resources