receive notification when file arrives

Problem

You want to get notified when a file arrives. Maybe it is coming in from somewhere else, or just is the result of some processing.

What ever the case, never sit there waiting for a file to arrive again! 🙂



Solution

Put the code under example tab, in a cron entry. See reference for more detail about cron.

Change the minutes, hour, filename and mail address! 🙂 You may need to use mail instead of mailx and ksh, etc instead of bash. But the basic syntax is the same. 🙂



Example



0-59 12 * * * bash -c "([ -f $HOME/test-touch ] &&
mailx -s "test-touch found at `date`" admin@acme.com



Reference

[tags]cron, UNIX Batch, UNIX Scripts, Unix Coding School[/tags]



UNIX debugging

Problem

  1. You want to debug a running process on Solaris?
  2. You want to debug running process on Linux?


Solution

See the example for debugging processes on Solaris or Linux – the syntax shows all system executes and reads.



Example


  1. truss -xall -vall -rall -t'read' -p PID #

    To run a command and have it produce debug info, run it prefix with the truss syntax:

    truss -xall -vall -rall -t'read' your command #

    Another good option when debugging, is to run truss and look at the opens. Or more specifically the failures – as this often highlights missing libraries or permissions issues.

    truss -t'open' -p PID #

  2. On Linux use strace, which takes basically the same options. You just use -e, like this:

    strace -e'open' ls
    open("/etc/ld.so.preload", O_RDONLY) = -1 ENOENT (No such file or directory)
    open("/etc/ld.so.cache", O_RDONLY) = 3
    open("/lib/tls/librt.so.1", O_RDONLY) = 3
    open("/lib/libtermcap.so.2", O_RDONLY) = 3
    open("/lib/libacl.so.1", O_RDONLY) = 3
    open("/lib/tls/libc.so.6", O_RDONLY) = 3
    open("/lib/tls/libpthread.so.0", O_RDONLY) = 3
    open("/lib/libattr.so.1", O_RDONLY) = 3
    open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
    open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY) = 3
    open("/etc/mtab", O_RDONLY) = 3
    open("/proc/meminfo", O_RDONLY) = 3



Reference

[tags]UNIX Debugging, Solaris Debugging, Linux Debugging, truss, strace, Unix Coding School[/tags]



Smallest AJAX example in the world?

Problem

Learn and demonstrate some simple AJAX.

To produce a clear, clean example – as small as possible.



Solution

Can a smaller working AJAX example be produced?

Always a believer in keeping it simple & find it easier to learn something complex, by making it as small as possible. See Example.



Example


  1. Click 'see demo'.
  2. Click image to see updated date.
  3. Click 'view the code'.

Here is the code:

<?php

if(isset($_GET['subreq'])) {
echo date('l dS of F Y h:i:s A');
exit(0);
}
?>
<img src=/icons/a.png onClick="javascript:
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch (E) { xmlhttp = false; }
}
@end @*/
if(!xmlhttp) { xmlhttp=new XMLHttpRequest(); }
xmlhttp.open('GET','index.php?subreq=y', true);
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState==4) { document.getElementById('output_div').innerHTML = xmlhttp.responseText; }
}
xmlhttp.send(null);
">
<div id='output_div'>click image above to put date here</div>

Or Click this link:



Reference

[tags]AJAX tutorial, AJAX, AJAX demonstration, PHP, Learn AJAX[/tags]

For excellent doco (as always) check out Wiki.



Shell Tips and Tricks

Problem

  1. You want to trace program execution on Solaris?
  2. You want to perform network tracing on Solaris?
  3. Perform a text dump of a binary file – or see ascii codes, etc?
  4. Generate some random data?
  5. List directory contents in 3 columns?
  6. Loop through a list of files and perform actions on them?
  7. Check for values in vars?
  8. Detach a process without it dying.
  9. Perform arithmetic in shell.


Solution

Some of the most useful Shell Tricks I’ve learned – check them out at Example – enjoy! 🙂



Example


  1. System process debug (solaris) all exec/reads

    truss -xall -vall -rall -t'read' -p PID

  2. Network trace connection from hostname (solaris)

    snoop -x0 src hostname

  3. Optical dump of a file, useful for seeing control chars, etc and lines with no newline.

    od -c filename

  4. Generate 512 bytes of random data, cat -ve escapes control chars.

    dd bs=1 count=512 if=/dev/urandom | cat -ve

  5. list directory contents, display in 3 columns.

    ls | paste - - -

  6. Loop through all files and perform an action on it.

    for na in * ; do echo $na ; done

  7. Check for values in vars.

    [[ $(echo $var | grep -ic "pattern") -eq 0 ]] &

    You could also go for "-eq 1" – does contain or -ne 0 for same, etc. Plus you could use || for "or" either in place of "&&" or after this block – same as if .. then "positive" else "negative" fi.

  8. Detach a process without it dying.

    disown [%1]

    nohup command > /tmp/logfile.log 2>&1&

    If you start a process with nohop (ignore the hup signal), it will not exist (usually) when your shell is exited. This can now be simulated on solaris, by backgrounding a process (with ctrl Z and bg) then typing disown. jobs command will show all backgrounded jobs. Ideally start commands like this, to avoid the shell exit problem:

  9. Perform arithmetic in shell.

    bc -l

    Binary Calculator in long format – for float, etc. Can also be automated like this, to convert between decimal and hex:

    i=0; while ((i<20)) ; do echo -n "$i: "; echo "base=10;obase=16;$i" | bc; ((i++)); done | paste - - - -
    0: 0 1: 1 2: 2 3: 3
    4: 4 5: 5 6: 6 7: 7
    8: 8 9: 9 10: A 11: B
    12: C 13: D 14: E 15: F
    16: 10 17: 11 18: 12 19: 13



Reference

[tags]Unix Shell Tips, Unix Shell Tricks, UNIX Arithmetic, Optical Dump, Unix Coding School[/tags]