I am running a small instance on Amazon EC2 with Ubuntu 12.04 LTS.
I have also setup Cloudwatch Alarm on the instance.
The problem is that CPU utilization goes above 90% sometimes and I get notification alarm for that.
I have set a cronjob on instance which runs at every minute and stores the top 3 running processes based on highest CPU usage in a log file. Cronjob is as below.
* * * * * ps -eo pcpu,pid,args --no-headers | sort -n -r | head -3 | perl -pe 'print scalar(localtime()), " ";' >> ps_log/log
But I can't see any process with high CPU usage when I run this command on cron log.
cat ps_log/log | sort -k 6 -n -r | head -10
Following is the latest result of cron log.
Tue May 13 17:44:01 2014 17.1 10171 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Tue May 13 18:06:01 2014 15.1 10502 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Tue May 13 13:28:01 2014 14.7 6526 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Tue May 13 09:56:01 2014 12.4 3277 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Tue May 13 18:06:01 2014 11.4 10508 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Wed May 14 02:32:36 2014 11.0 16936 ps -eo pcpu,pid,args --no-headers
Tue May 13 13:32:01 2014 10.3 6619 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Tue May 13 18:06:01 2014 10.2 10501 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Tue May 13 11:08:01 2014 9.6 4802 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Wed May 14 02:58:07 2014 8.5 17268 ps -eo pcpu,pid,args --no-headers
I can show the two Alarm results and the result of cronlog at which the Alarm notification came.
Reason for State Change: Threshold Crossed: 1 datapoint (96.72) was greater than or equal to the threshold (80.0).
Timestamp: Tuesday 13 May, 2014 15:42:09 UTC
Cronlog:
Tue May 13 15:39:20 2014 2.0 8481 perl -pe print scalar(localtime()), " ";
Tue May 13 15:39:20 2014 1.6 8478 ps -eo pcpu,pid,args --no-headers
Tue May 13 15:39:20 2014 1.2 8004 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Tue May 13 15:41:08 2014 1.7 8527 /opt/lampp/sbin/mysqld --basedir=/opt/lampp --datadir=/opt/lampp/var/mysql --plugin-dir=/opt/lampp/lib/mysql/plugin --user=nobody --log-error=/opt/lampp/var/mysql/ip-10-178-52-49.err --pid-file=/opt/lampp/var/mysql/ip-10-178-52-49.pid --socket=/opt/lampp/var/mysql/mysql.sock --port=3306
Tue May 13 15:41:08 2014 1.5 8547 ps -eo pcpu,pid,args --no-headers
Tue May 13 15:41:08 2014 0.9 8003 [httpd] <defunct>
Tue May 13 15:43:01 2014 6.0 8578 sort -n -r
Tue May 13 15:43:15 2014 5.0 8577 ps -eo pcpu,pid,args --no-headers
Tue May 13 15:43:24 2014 3.3 8579 head -3
Tue May 13 15:44:21 2014 1.2 8527 /opt/lampp/sbin/mysqld --basedir=/opt/lampp --datadir=/opt/lampp/var/mysql --plugin-dir=/opt/lampp/lib/mysql/plugin --user=nobody --log-error=/opt/lampp/var/mysql/ip-10-178-52-49.err --pid-file=/opt/lampp/var/mysql/ip-10-178-52-49.pid --socket=/opt/lampp/var/mysql/mysql.sock --port=3306
Reason for State Change: Threshold Crossed: 1 datapoint (96.72) was greater than or equal to the threshold (80.0).
Timestamp: Tuesday 13 May, 2014 15:42:09 UTC
Cronlog:
Tue May 13 15:39:20 2014 2.0 8481 perl -pe print scalar(localtime()), " ";
Tue May 13 15:39:20 2014 1.6 8478 ps -eo pcpu,pid,args --no-headers
Tue May 13 15:39:20 2014 1.2 8004 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Tue May 13 15:41:08 2014 1.7 8527 /opt/lampp/sbin/mysqld --basedir=/opt/lampp --datadir=/opt/lampp/var/mysql --plugin-dir=/opt/lampp/lib/mysql/plugin --user=nobody --log-error=/opt/lampp/var/mysql/ip-10-178-52-49.err --pid-file=/opt/lampp/var/mysql/ip-10-178-52-49.pid --socket=/opt/lampp/var/mysql/mysql.sock --port=3306
Tue May 13 15:41:08 2014 1.5 8547 ps -eo pcpu,pid,args --no-headers
Tue May 13 15:41:08 2014 0.9 8003 [httpd] <defunct>
Tue May 13 15:43:01 2014 6.0 8578 sort -n -r
Tue May 13 15:43:15 2014 5.0 8577 ps -eo pcpu,pid,args --no-headers
Tue May 13 15:43:24 2014 3.3 8579 head -3
Tue May 13 15:44:21 2014 1.2 8527 /opt/lampp/sbin/mysqld --basedir=/opt/lampp --datadir=/opt/lampp/var/mysql --plugin-dir=/opt/lampp/lib/mysql/plugin --user=nobody --log-error=/opt/lampp/var/mysql/ip-10-178-52-49.err --pid-file=/opt/lampp/var/mysql/ip-10-178-52-49.pid --socket=/opt/lampp/var/mysql/mysql.sock --port=3306
Tue May 13 15:44:21 2014 0.7 8569 CRON
Tue May 13 15:44:21 2014 0.7 8501 /opt/lampp/bin/httpd -k start -DSSL -DPHP5 -E /opt/lampp/logs/error_log
Tue May 13 15:44:21 2014 1.2 8527 /opt/lampp/sbin/mysqld --basedir=/opt/lampp --datadir=/opt/lampp/var/mysql --plugin-dir=/opt/lampp/lib/mysql/plugin --user=nobody --log-error=/opt/lampp/var/mysql/ip-10-178-52-49.err --pid-file=/opt/lampp/var/mysql/ip-10-178-52-49.pid --socket=/opt/lampp/var/mysql/mysql.sock --port=3306
Tue May 13 15:44:21 2014 0.7 8569 CRON
Now is there any way I can catch the process with high CPU usage? A website is hosted on the instance which has very low traffic.
Any help would be appreciated.
A couple of things:
You may see a disconnect between reported cpu usage on linux in a
virtual machine and what Amazon reports as the real cpu usage. Note
that the latter is correct. Cpu usage stats monitoring via ps and top are
unreliable, a good explanation resides here:
http://www.axibase.com/cloud/2010/07/22/ec2-monitoring-the-case-of-stolen-cpu/
Regardless of the accuracy of the top and ps commands, something is
causing cpu to spike. Ps and top should at least tell you which processes are using the most. Instead of calling ps once a minute which may
miss the offending process, why not run it in a loop from a bash
script with a short loop time (like every 10 seconds)? Redirect it
to a log file and and you should be able to find a ps or top entry
within a few seconds of the alarm. Something like this:
while :
do
date
echo
ps -eo pcpu,pid,args --no-headers
echo
top -c -b -n 1
echo
sleep 10
done
Related
I want to write a simple shell script that downloads the latest ISO image of Webconverger and changes the boot parameters of grub in /boot/live.cfg.
So this is what I have done so far:
#!/bin/bash
# 1. Download the ISO
wget https://build.webconverger.com/latest.iso
# 2. Mount the ISO and create a development folder
sudo mkdir /mnt/webconverger
sudo mount -o loop latest.iso /mnt/webconverger
mkdir devfolder
rsync -av /mnt/webconverger/ devfolder/
sudo umount /mnt/webconverger
sudo rm -r /mnt/webconverger
# 3. Change the file in the development folder.
chmod -R 777 devfolder/
rm devfolder/boot/live.cfg
echo \
"DEFAULT de
label de
menu label Start
kernel /live/vmlinuz
append initrd=/live/initrd.img boot=live skipconfig quiet splash components=gitfs net.ifnames=0 bootfrom=removable locale=de xkb=-layout%20de homepage=https://www.google.de/" \
> devfolder/boot/live.cfg
# 4. Create a new ISO image
mkisofs -o new.iso -l devfolder
rm -r devfolder/
The generated ISO does not boot.
I asume the reasons for the problem are, that I had to run the whole script with root privileges and that I need a more complex mkisofs command.
Does anybody know how to generate a proper ISO image for Webconverger with a shell script? Or is there an alternative method for the whole process? Something like ISO Master but for the command line?
Update
I have replaced the code snippets with the actual shell script. So it is easier to test. And here is the command line output (without the wget part):
mount: /dev/loop0 is write-protected, mounting read-only
sending incremental file list
./
boot/
boot/boot.bin
boot/boot.cat
boot/live.cfg
boot/syslinux/
boot/syslinux/ldlinux.c32
boot/syslinux/libcom32.c32
boot/syslinux/libutil.c32
boot/syslinux/splash.png
boot/syslinux/syslinux.cfg
boot/syslinux/vesainfo.c32
boot/syslinux/vesamenu.c32
live/
live/initrd.img
live/vmlinuz
live/filesystem.git/
live/filesystem.git/HEAD
live/filesystem.git/config
live/filesystem.git/description
live/filesystem.git/packed-refs
live/filesystem.git/shallow
live/filesystem.git/branches/
live/filesystem.git/hooks/
live/filesystem.git/hooks/applypatch-msg.sample
live/filesystem.git/hooks/commit-msg.sample
live/filesystem.git/hooks/post-update.sample
live/filesystem.git/hooks/pre-applypatch.sample
live/filesystem.git/hooks/pre-commit.sample
live/filesystem.git/hooks/pre-push.sample
live/filesystem.git/hooks/pre-rebase.sample
live/filesystem.git/hooks/pre-receive.sample
live/filesystem.git/hooks/prepare-commit-msg.sample
live/filesystem.git/hooks/update.sample
live/filesystem.git/info/
live/filesystem.git/info/exclude
live/filesystem.git/objects/
live/filesystem.git/objects/info/
live/filesystem.git/objects/pack/
live/filesystem.git/objects/pack/pack-1aa978c13266848f4644754d1875dc90ec4bfcdb.idx
live/filesystem.git/objects/pack/pack-1aa978c13266848f4644754d1875dc90ec4bfcdb.pack
live/filesystem.git/refs/
live/filesystem.git/refs/heads/
live/filesystem.git/refs/heads/master
live/filesystem.git/refs/remotes/
live/filesystem.git/refs/remotes/origin/
live/filesystem.git/refs/remotes/origin/HEAD
live/filesystem.git/refs/tags/
live/filesystem.git/refs/tags/initial-revision
sent 598,528,659 bytes received 726 bytes 239,411,754.00 bytes/sec
total size is 598,380,033 speedup is 1.00
I: -input-charset not specified, using utf-8 (detected in locale settings)
1.71% done, estimate finish Tue Jul 10 20:12:54 2018
3.42% done, estimate finish Tue Jul 10 20:12:54 2018
5.13% done, estimate finish Tue Jul 10 20:13:13 2018
6.84% done, estimate finish Tue Jul 10 20:13:08 2018
8.55% done, estimate finish Tue Jul 10 20:13:05 2018
10.26% done, estimate finish Tue Jul 10 20:13:03 2018
11.97% done, estimate finish Tue Jul 10 20:13:02 2018
13.68% done, estimate finish Tue Jul 10 20:13:01 2018
15.39% done, estimate finish Tue Jul 10 20:13:00 2018
17.10% done, estimate finish Tue Jul 10 20:12:59 2018
18.81% done, estimate finish Tue Jul 10 20:12:59 2018
20.52% done, estimate finish Tue Jul 10 20:12:58 2018
22.23% done, estimate finish Tue Jul 10 20:12:58 2018
23.94% done, estimate finish Tue Jul 10 20:12:58 2018
25.65% done, estimate finish Tue Jul 10 20:12:57 2018
27.36% done, estimate finish Tue Jul 10 20:12:57 2018
29.07% done, estimate finish Tue Jul 10 20:12:57 2018
30.78% done, estimate finish Tue Jul 10 20:12:57 2018
32.49% done, estimate finish Tue Jul 10 20:12:57 2018
34.20% done, estimate finish Tue Jul 10 20:12:56 2018
35.91% done, estimate finish Tue Jul 10 20:12:56 2018
37.62% done, estimate finish Tue Jul 10 20:12:56 2018
39.33% done, estimate finish Tue Jul 10 20:12:56 2018
41.04% done, estimate finish Tue Jul 10 20:12:56 2018
42.75% done, estimate finish Tue Jul 10 20:12:56 2018
44.46% done, estimate finish Tue Jul 10 20:12:56 2018
46.17% done, estimate finish Tue Jul 10 20:12:56 2018
47.88% done, estimate finish Tue Jul 10 20:12:56 2018
49.59% done, estimate finish Tue Jul 10 20:12:56 2018
51.30% done, estimate finish Tue Jul 10 20:12:55 2018
53.02% done, estimate finish Tue Jul 10 20:12:55 2018
54.72% done, estimate finish Tue Jul 10 20:12:55 2018
56.44% done, estimate finish Tue Jul 10 20:12:55 2018
58.14% done, estimate finish Tue Jul 10 20:12:55 2018
59.86% done, estimate finish Tue Jul 10 20:12:55 2018
61.56% done, estimate finish Tue Jul 10 20:12:55 2018
63.28% done, estimate finish Tue Jul 10 20:12:55 2018
64.98% done, estimate finish Tue Jul 10 20:12:55 2018
66.70% done, estimate finish Tue Jul 10 20:12:55 2018
68.40% done, estimate finish Tue Jul 10 20:12:55 2018
70.12% done, estimate finish Tue Jul 10 20:12:55 2018
71.82% done, estimate finish Tue Jul 10 20:12:55 2018
73.54% done, estimate finish Tue Jul 10 20:12:55 2018
75.24% done, estimate finish Tue Jul 10 20:12:55 2018
76.96% done, estimate finish Tue Jul 10 20:12:55 2018
78.66% done, estimate finish Tue Jul 10 20:12:55 2018
80.38% done, estimate finish Tue Jul 10 20:12:55 2018
82.08% done, estimate finish Tue Jul 10 20:12:55 2018
83.80% done, estimate finish Tue Jul 10 20:12:55 2018
85.50% done, estimate finish Tue Jul 10 20:12:55 2018
87.22% done, estimate finish Tue Jul 10 20:12:55 2018
88.92% done, estimate finish Tue Jul 10 20:12:55 2018
90.64% done, estimate finish Tue Jul 10 20:12:55 2018
92.34% done, estimate finish Tue Jul 10 20:12:55 2018
94.06% done, estimate finish Tue Jul 10 20:12:55 2018
95.76% done, estimate finish Tue Jul 10 20:12:55 2018
97.48% done, estimate finish Tue Jul 10 20:12:55 2018
99.18% done, estimate finish Tue Jul 10 20:12:55 2018
Total translation table size: 0
Total rockridge attributes bytes: 0
Total directory bytes: 30720
Path table size(bytes): 222
Max brk space used 2b000
292387 extents written (571 MB)
And this what I get in VirtualBox:
Click here!
So the full answer to the question is:
#!/bin/bash
# 1. Download the ISO
wget https://build.webconverger.com/latest.iso
# 2. Mount the ISO and create a development folder
sudo mkdir /mnt/webconverger
sudo mount -o loop latest.iso /mnt/webconverger
mkdir devfolder
rsync -av /mnt/webconverger/ devfolder/
sudo umount /mnt/webconverger
sudo rm -r /mnt/webconverger
# 3. Change the file in the development folder.
echo \
"DEFAULT de
label de
menu label Start
kernel /live/vmlinuz
append initrd=/live/initrd.img boot=live skipconfig quiet splash components=gitfs net.ifnames=0 bootfrom=removable locale=de xkb=-layout%20de homepage=https://duckduckgo.com nobrand" \
| sudo tee devfolder/boot/live.cfg
# 4. Create a new ISO image
sudo mkisofs -r -J -l -V "Webconverger" -b boot/boot.bin -c boot/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o new.iso devfolder
sudo isohybrid new.iso
sudo rm -r devfolder/
For some reason dd did not work for Webconverger images. Even the with the untouched latest.iso it was not possible to create a bootable USB flash drive. So I used Win32DiskImager.
What you want to do is to generate an El Torito bootable CD with an MBR in its first bytes.
mkisofs(8) needs some special options to create an "El Torito" bootable CD:
mkisofs -r -J -l -V "Webconverger" -b boot/boot.bin -c boot/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o new.iso devfolder
Explaination:
-r -J: Enable Joliet and Rock Ridge meta data.
-l: Allow long filenames.
-V: Set volume ID. Optional.
-b: Specify the "El Torito" boot image.
-c: Specify the "El Torito" boot catalog.
-no-emul-boot: Specify that this is an "El Torito" bootable CD.
-boot-load-size: number of 512 bytes sector to load (4 is what usually works)
-boot-info-table: modify the original boot image appending the Boot Info Table
Then, if you want to be able to just dd the file on a USB drive you will need to write an MBR onto the ISO:
isohybrid new.iso
I have a linux server and there contains an important script xyz.sh. At times there will be 10-50 users logged into that machine. Is it possible to find who is running the script? Also, is it possible to get a log who all have ran the script xyz.sh; means is it possible to extract a history of script run?
For a simple on-the-fly check about the owners' script while it's runnging you can use:
$ ps -e -o euid,pid,euser,state,command | grep "xyz.sh"|grep -v grep
0 31096 root S /bin/bash ./xyz.sh
1000 31030 ale S /bin/bash ./xyz.sh
It should be possible to log the ps output with this script:
#!/bin/bash
SECONDS=5
TARGET=xyz.sh
OUT=/var/tmp/xyz_history.log
while true
do
sleep $SECONDS
echo "$(date '+TIME:%H:%M:%S';ps -e -opid,user,command|grep $TARGET | grep -v grep)"
done >> $OUT
exit 0
The output:
$ tail -f /var/tmp/xyz_history.log
TIME:14:13:37
496 postgres /bin/bash ./xyz.sh
625 ale /bin/bash ./xyz.sh
32137 root /bin/bash ./xyz.sh
TIME:14:13:38
496 postgres /bin/bash ./xyz.sh
625 ale /bin/bash ./xyz.sh
32137 root /bin/bash ./xyz.sh
TIME:14:13:39
496 postgres /bin/bash ./xyz.sh
625 ale /bin/bash ./xyz.sh
TIME:14:13:40
496 postgres /bin/bash ./xyz.sh
625 ale /bin/bash ./xyz.sh
...
This is not a clean solution of course. If you can install packages on the system and run commands as superuser, a better solution is using lastcomm:
# lastcomm xyz.sh
xyz.sh X ale pts/1 0.00 secs Fri Sep 11 14:12
xyz.sh X root pts/3 0.00 secs Fri Sep 11 14:00
xyz.sh X ale pts/4 0.00 secs Fri Sep 11 14:08
xyz.sh X ale pts/4 0.00 secs Fri Sep 11 14:00
xyz.sh X root pts/4 0.00 secs Fri Sep 11 13:54
xyz.sh X ale pts/1 0.00 secs Fri Sep 11 13:51
xyz.sh X root pts/3 0.00 secs Fri Sep 11 13:42
xyz.sh X ale pts/1 0.00 secs Fri Sep 11 13:36
xyz.sh X ale pts/1 0.00 secs Fri Sep 11 13:36
xyz.sh X ale pts/1 0.00 secs Fri Sep 11 13:36
xyz.sh X ale pts/1 0.00 secs Fri Sep 11 13:36
xyz.sh X postgres pts/1 0.00 secs Fri Sep 11 13:36
xyz.sh X ale pts/1 0.00 secs Fri Sep 11 13:36
xyz.sh X root pts/1 0.00 secs Fri Sep 11 13:36
xyz.sh X ale pts/1 0.00 secs Fri Sep 11 13:36
It's possible install the command lastcomm from the psacct (centos/redhat) or acct package (debian/ubuntu/OpenSuse).
I am trying to run a crontab which executes a bash script which triggers mail command. I have done only one entry in crontab for my user. Also doing ps ax | grep cron results only one crond process. I don't know why i am getting mails twice
Cron logs
Aug 7 14:38:10 centos crond[29299]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 85% if used.)
Aug 7 14:38:11 centos crond[29299]: (CRON) INFO (running with inotify support)
Aug 7 14:38:11 centos crond[29299]: (CRON) INFO (#reboot jobs will be run at computer's startup.)
Aug 7 14:40:01 centos CROND[29376]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 7 14:50:01 centos CROND[29940]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 7 14:59:01 centos CROND[30388]: (test_user) CMD (bash /home/test_user/dev/mail_test_user.sh)
Aug 7 15:00:01 centos CROND[30585]: (test_user) CMD (bash /home/test_user/dev/mail_test_user.sh)
Aug 7 15:00:01 centos CROND[30586]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Aug 7 15:01:01 centos CROND[30775]: (root) CMD (run-parts /etc/cron.hourly)
Aug 7 15:01:01 centos run-parts(/etc/cron.hourly)[30775]: starting 0anacron
Aug 7 15:01:01 centos run-parts(/etc/cron.hourly)[30787]: finished 0anacron
Aug 7 15:01:01 centos run-parts(/etc/cron.hourly)[30775]: starting 0yum-hourly.cron
Aug 7 15:01:01 centos run-parts(/etc/cron.hourly)[30795]: finished 0yum-hourly.cron
Crontab list
#reboot autossh -M 8000 -f -N -R 9000:localhost:22 remote_user#192.168.0.3
#reboot autossh -M 8002 -f -N -R 9001:localhost:5901 remote_user#192.168.0.3
#reboot autossh -M 8004 -f -N -R 9002:localhost:80 remote_user#192.168.0.3
0,59 * * * * bash /home/vikas/dev/mail_test_user.sh
Contents of mail_test_user.sh
echo "I am up :)))" | mail -s "Notification : test_user#centos.localdomain [STAG]" foobaar22#gmail.com
ps aux | grep cron results
root 29299 0.0 0.0 126300 1692 ? Ss 14:38 0:00 /usr/sbin/crond -n
test_user 31650 0.0 0.0 112640 964 pts/0 S+ 15:13 0:00 grep --color=auto cron
Any luck why the hack this is happening ?
I also tried rebooting and restarting crond but not working.
The second line is result of your grep command. This is process of grep command not your cronjob process. Because it matches your search criteria.
The error is in setting up cronjob it should be */59 instead of 0/59.
Is it possible to get the start time of an old running process? It seems that ps will report the date (not the time) if it wasn't started today, and only the year if it wasn't started this year. Is the precision lost forever for old processes?
You can specify a formatter and use lstart, like this command:
ps -eo pid,lstart,cmd
The above command will output all processes, with formatters to get PID, command run, and date+time started.
Example (from Debian/Jessie command line)
$ ps -eo pid,lstart,cmd
PID CMD STARTED
1 Tue Jun 7 01:29:38 2016 /sbin/init
2 Tue Jun 7 01:29:38 2016 [kthreadd]
3 Tue Jun 7 01:29:38 2016 [ksoftirqd/0]
5 Tue Jun 7 01:29:38 2016 [kworker/0:0H]
7 Tue Jun 7 01:29:38 2016 [rcu_sched]
8 Tue Jun 7 01:29:38 2016 [rcu_bh]
9 Tue Jun 7 01:29:38 2016 [migration/0]
10 Tue Jun 7 01:29:38 2016 [kdevtmpfs]
11 Tue Jun 7 01:29:38 2016 [netns]
277 Tue Jun 7 01:29:38 2016 [writeback]
279 Tue Jun 7 01:29:38 2016 [crypto]
...
You can read ps's manpage or check Opengroup's page for the other formatters.
The ps command (at least the procps version used by many Linux distributions) has a number of format fields that relate to the process start time, including lstart which always gives the full date and time the process started:
# ps -p 1 -wo pid,lstart,cmd
PID STARTED CMD
1 Mon Dec 23 00:31:43 2013 /sbin/init
# ps -p 1 -p $$ -wo user,pid,%cpu,%mem,vsz,rss,tty,stat,lstart,cmd
USER PID %CPU %MEM VSZ RSS TT STAT STARTED CMD
root 1 0.0 0.1 2800 1152 ? Ss Mon Dec 23 00:31:44 2013 /sbin/init
root 5151 0.3 0.1 4732 1980 pts/2 S Sat Mar 8 16:50:47 2014 bash
For a discussion of how the information is published in the /proc filesystem, see
https://unix.stackexchange.com/questions/7870/how-to-check-how-long-a-process-has-been-running
(In my experience under Linux, the time stamp on the /proc/ directories seem to be related to a moment when the virtual directory was recently accessed rather than the start time of the processes:
# date; ls -ld /proc/1 /proc/$$
Sat Mar 8 17:14:21 EST 2014
dr-xr-xr-x 7 root root 0 2014-03-08 16:50 /proc/1
dr-xr-xr-x 7 root root 0 2014-03-08 16:51 /proc/5151
Note that in this case I ran a "ps -p 1" command at about 16:50, then spawned a new bash shell, then ran the "ps -p 1 -p $$" command within that shell shortly afterward....)
As a follow-up to Adam Matan's answer, the /proc/<pid> directory's time stamp as such is not necessarily directly useful, but you can use
awk -v RS=')' 'END{print $20}' /proc/12345/stat
to get the start time in clock ticks since system boot.1
This is a slightly tricky unit to use; see also convert jiffies to seconds for details.
awk -v ticks="$(getconf CLK_TCK)" 'NR==1 { now=$1; next }
END { printf "%9.0f\n", now - ($20/ticks) }' /proc/uptime RS=')' /proc/12345/stat
This should give you seconds, which you can pass to strftime() to get a (human-readable, or otherwise) timestamp.
awk -v ticks="$(getconf CLK_TCK)" 'NR==1 { now=$1; next }
END { print strftime("%c", systime() - (now-($20/ticks))) }' /proc/uptime RS=')' /proc/12345/stat
Updated with some fixes from Stephane Chazelas in the comments; thanks as always!
If you only have Mawk, maybe try
awk -v ticks="$(getconf CLK_TCK)" -v epoch="$(date +%s)" '
NR==1 { now=$1; next }
END { printf "%9.0f\n", epoch - (now-($20/ticks)) }' /proc/uptime RS=')' /proc/12345/stat |
xargs -i date -d #{}
1 man proc; search for starttime.
ls -ltrh /proc | grep YOUR-PID-HERE
For example, my Google Chrome's PID is 11583:
ls -l /proc | grep 11583
dr-xr-xr-x 7 adam adam 0 2011-04-20 16:34 11583
$ ps -p 182454 -o lstart=
Mon Oct 18 17:26:44 2021
But can I get the answer in epoch seconds?
ps -eo pid,cmd,lstart | grep YOUR-PID-HERE
ps -eo pid,etime,cmd|sort -n -k2
use the command ls -ld /proc/process_id where process_id can be find using top command
I want to view the ORA errors in alertlogfile of past 7 (monday-sunday)days,
by writting in shell scripts.
Can anybody help me.
Thanks
Something like:
sed -n -e '/start_time/,/end_time/ {/ORA/ p}' logfile
or with awk
$ start="Fri Feb 27 08:00:00 2009"
$ end="Fri Mar 6 08:00:00 2009"
$ awk -v prev="$start" -v last="$end" '$0 ~ prev,$0 ~ last' logfile
A more sophisticated script looking for last date entries in ORA file is available here, but also at dba-oracle.com
This does not answer exactly your request but might give you some clues to start your own script.
I want the scripts which give output as follows (one week errors) and it should be mail to my id.
Sat Mar 14 10:30:51 IST 2009
ORA-01157: cannot identify/lock data file 2 - see DBWR trace file
Sat Mar 12 12:35:06 IST 2009
ORA-01110: data file 2: '/u02/oradata/Globe/undotbs01.dbf'
Sat Mar 10 09:54:05 IST 2009
ORA-27037: unable to obtain file status
Sat Mar 08 :15:02 IST 2009
ORA-1157 signalled during: ALTER DATABASE OPEN...
Sat Mar 07 12:35:51 IST 2009
ORA-01157: cannot identify/lock data file 2 - see DBWR trace file