![]() ESN 72240-090410-989372-77 |
|
Document Name: Shell script used time Document Description: Shell script used time2009/04/10 I often use "ls -lut" in troubleshooting. What that does is show you the last time a file was "used", which can be very helpful in tracing whether or not something happened when you expected it to. However, when it comes to shell scripts, there are some quirks you need to know about. For example, suppose you have a a cron job set to fire off at 3:00 AM. That job in turn may fire off other scripts or programs but your main concern is "did it run?", so when you log in the next morning you issue an "ls -lut" on it. You expect to see 3:00 for the time stamp, but you may not. To illustrate this, let's take a simple script "t". Here it is: sleep 120 We'll also create a testing harness, "tt": date rm t echo "sleep 120" > t ls -lut t echo "Running bash" /bin/bash ./t ls -lut t echo "Running sh" /bin/sh ./t ls -lut t echo "Running ksh" /bin/ksh ./t ls -lut t echo "Running csh" /bin/csh ./t ls -lut t The output from that script shows that for these shells, the "used" time for "t" gets set to the time it stops, not when it starts. That may actually be useful - especially if you didn't expect the script to run as long as it did. On the other hand, if this wasn't run by a cron job and you need to know when it was run, you don't have that: you only know the end time. Note that a Perl script will show only the start time, as will a binary executable:
$ cat t.pl
#!/usr/bin/perl
sleep 120;
exit 0;
$ touch t.pl;ls -lut t.pl;./t.pl;ls -lut t.pl
-rwxr-xr-x 1 apl apl 35 Apr 10 12:09 t.pl
-rwxr-xr-x 1 apl apl 35 Apr 10 12:09 t.pl
$ cat tc.c
#include <stdio.h>
main ()
{
printf("Hello World\n");
sleep(120);
}
$ make tc
cc tc.c -o tc
$ ls -lut tc;./tc; ls -lut tc
-rwxr-xr-x 1 apl apl 12608 Apr 10 12:09 tc
Hello World
-rwxr-xr-x 1 apl apl 12608 Apr 10 12:09 tc
Author: Anthony Lawrence - Contact Author Publisher: Anthony Lawrence Licensee Name: Anthony Lawrence Reference URL: http://aplawrence.com/Linux/used_times.html Copyright: All Rights Reserved Registration Date: 4/10/2009 4:51:21 PM UTC Views: 660 |
