APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Shell script used time

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":

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
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");
$ 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

Got something to add? Send me email.

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Anthony Lawrence

Kerio Samepage

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

The worst crime against working people is a company which fails to operate at a profit. (Samuel Gompers)

Tough times never last, but tough people do. (Robert H. Schuller)

This post tagged: