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

Shell script used time


Some material is very old and may be incorrect today

© April 2009 Anthony Lawrence

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
 

If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Shell script used time


Inexpensive and informative Apple related e-books:

Photos for Mac: A Take Control Crash Course

Take control of Apple TV, Second Edition

iOS 8: A Take Control Crash Course

iOS 10: A Take Control Crash Course

Take Control of Parallels Desktop 12





More Articles by © Anthony Lawrence





Printer Friendly Version

Have you tried Searching this site?

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


Printer Friendly Version





A man can be destroyed but not defeated. (Ernest Hemingway)




Linux posts

Troubleshooting posts


This post tagged:

Linux

Scripting

Shell

Troubleshooting



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode