how to find linux process uptime in days - shell

Can any one please let me know how can I find the uptime of a unix process in days?
I am able to get the date time of the process with below command
[zdkafadm#toplxkafp005 sa]$ ps -eo pid,lstart,cmd |grep server.properties | grep -v auto | awk 'NR==1;'|awk '{print $3" "$4" " $5" "$6}'
Oct 29 12:47:24 2017
Somehow I need the output something like 15 days.
Thanks,
Chandan

The easiest (sticking within shell) is using the 'dateutils' package. Otherwise perl has some fine date/time modules.

This is a fun solution:
ps -o etime= ${pid?} | rev |
sed -e 's/^/ .sdnoces /' -e 's/:/ ,setunim /' \
-e 's/:/ ,sruoh /' -e 's/-/ ,syad /' | rev

Related

Unable to echo variable value

Assigning the output of sed command in a variable but unable to print its value, the command works fine:-
uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'
but I assigned a variable for its out like below:-
abc=uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'
and calling variable is not pulling the value.
Tried like below:-
echo {"$abc"}
printf "$abc"
echo "${abc}"
Kindly suggest the syntax for output.
abc=uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'
Actually we need to pull the uptime value for only number of days on AIX server and call that value to form a report of servers which will show number of days server uptime for AIX servers. Need to know how to call the variable value and embedd it in a shell script.
Depends on your shell, but for most sh-ish variants:
abc=$(uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g')
Because you are not storing the final evaluation in abc.
Try
abc=$(uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g')

How can I pipe the results of grep to a perl one liner?

I have a grep command that find the files that need a value replaced. Then I have a perl one liner that needs to be executed on each file to replace a variables found in that file.
How can I pipe the results of my grep command to the perl one liner?
grep -Irc "/env/file1/" /env/scripts/ | cut -d':' -f1 | sort | uniq
/env/scripts/config/MainDocument.pl
/env/scripts/config/MainDocument.pl2
/env/scripts/config/MainDocument.pl2.bak
perl -p -i.bak -e 's{/env/file1/}{/env/file2/}g' /env/scripts/config/MainDocument.pl
Thanks for your help.
With the $(...) bash syntax.
perl -p -i.bak -e 's{/env/file1/}{/env/file2/}g' $(grep -Irc "/env/file1/" /env/scripts/ | cut -d':' -f1 | sort | uniq)
I'd forget the perl one liner to use xargs and sed instead.
grep -Irc "/env/file1/" /env/scripts/ | cut -d':' -f1 | sort | uniq | xargs sed -ibak ':/env/file1/:/env/file2/:'

How to find value of a key on tail -f

My log files are in key-value format. I want to find value of a particular key on tail -f ..
Suppose one of the line in log is:
ts=2016-12-23-18-31-34-849 | deviceType=LENOVO Lenovo A6000 | elapsed=11 | firstHomePage=null | installId=37797b61-0bb1-4c1a-844c-5904c7e83de8 | ip=157.48.104.146
ts=2016-12-23-18-31-34-849 | deviceType=LENOVO Lenovo A6000 | elapsed=15 | firstHomePage=null | installId=37797b61-0bb1-4c1a-844c-5904c7e83de8 | ip=157.48.104.146
I am not sure how do I pipe output of my tail -f so that output should be following
11
15
Use GNU grep with the --line-buffered command to buffer stdout as it arrives in case of continuously growing file. The -o flag for matching only the pattern and -P to enable perl style regEx captures.
tail -f file | grep --line-buffered -oP "elapsed=\K(\d+)"
11
15
From the man grep page,
--line-buffered
Use line buffering on output.
Try grep:
tail log_file | grep -o '\<elapsed=[^[:space:]]*' | cut -d= -f2
awk -F'[=|]' '{print $6}' file
11
15

ps aux | grep returns pid for itself too

I am using this command to get the process ID of another command:
ps aux | grep 7000.conf | awk '{print $2}'
This will return two PIDs:
7731
22125
I only want the first one. The second is the PID for grep in the above command. Thanks in advance to any one who knows how to alter the above command to return just the first pid.
p.s. open to a new command that does the same thing
In this particular case, escaping the . to what I assume it was meant to do should work:
ps aux | grep '7000\.conf' | awk '{print $2}'
Alternatively, exclude grep:
ps aux | grep 7000.conf | grep -v grep | awk '{print $2}'
ps aux | grep "[7]000.conf" will work as well.

Remove everything after a pattern (.com)

Driving myself nuts. I am trying to get just the domain name (http://www.example.com) out of access.log. What the log looks like:
tail access.log
Fri, 13 Jul 2012 20:32:03 -0700,INFO,6fgmd8fk,params,http://www.example.com/images/CIV-260.jpg|
I have tried many variations of this one-liner (with sed and awk):
tail -4 access.log |grep http |awk {'print $6'} |cut -c28- |awk '$1>".com"' |sort |uniq
http://www.example.com/2713-7807.jpg|
http://www.example.com/2713-7808.jpg|
http://barfoo.com/img/14616_20120711182527.jpg|
http://foobar.com/css/14616_20120713142151.css|
I am stuck.
Maybe just
awk -F/ '{print $3}'
if you don't have more '/' than you example shows.
Notice this is just the domain name, as your question asks.
Using grep:
grep -Po '(?<=http://)[^/]+' access.log | sort -u
If you want to have http:// as a part of domain name,
grep -Po 'http://[^/]+' access.log | sort -u
Using sed:
sed -n 's|.*\(http://[^/]*\)/.*|\1|p' access.log | sort -u

Resources