vv
I collected some best practices for long running commands over the years that I consolidated into a wrapper called vv
I have a little web page for it here:
http://joshuacox.github.io/vv/
I’ll tear it apart here if anyone cares to know what is going on, and I’ll give some basics to writing bash scripts as well.
Shebang
First we give the shebang line.
#!/bin/bash
Which starts with ‘shee’ or ‘#’ and ‘bang’ or ‘!’. Upon hearing these names for these symbols the first time I wanted to exclaim I hereby declare that no one is to call the ‘!’ an ‘exclamation point’, from here on out it is to be known as bang! And hash is now know as ‘shee’
.
Anyhow, after your shebang you call an absolute path to your interpreter, which in this case is ‘bash’ and bash lives at /bin/bash
on most systems, if not try which bash
, and the which command will tell you where bash lives on your system
Comments in Bash
we then have some comments. Comments start with a shee or ‘#’
# this is a wrapper script that shows
# time info and beeps after said thing is done
# and nice and ionice for niceness
Those lines are ignore by bash, they are merely ‘comments’ for any human who happens to be reading my code (in this case you)
The meat of the matter
Then comes the big line that does all the magic
/usr/bin/time -v nice ionice -c3 "$@"
let’s tear this apart into pieces
/usr/bin/time -v
This first line uses the time utility in verbose mode -v
to time the command that is about to be executed
nice
the nice command will ensure our process is polite and lets most other processes go ahead of it, meaning that it will politely wait until the processor is free to do it’s work
ionice -c3
ionice is similar to nice, except that it keeps our process from using disk input/output, and lets other processes go ahead of it, again, patiently awaiting the disk to become idle before asking it do work
"$@"
This symbol $@
to bash means ‘collectively all the arguments’, so if you were to call vv like this vv cat one.txt two.txt
it would pass ‘cat one.txt two.txt’ in as that $@
in this way vv becomes a wrapper for my commands
It is important to note that all commands before the $@
are wrappers in themselves, in that they ‘wrap’ up the commands you give to them, and in a chain like that each one wraps the previous one up, so here it is again in one line:
/usr/bin/time -v nice ionice -c3 "$@"
File system syncs and you
The linux kernel loves to lie to processes and tell them their job is done and for them to go away, to do this it says “yes, your write is done now go away”, even if this write is still in memory and yet to be actually committed to disc, this has the effect of the overall system being much faster as processes come up and go down with out having to await the commits to disk, so long as everything doesn’t crash this works great, but I like to wait before that happens before my script ends so I place a sync
in here and time it for kicks, just to see how much the kernel was hiding from me
# indicate we moved on to sync
echo "now syncing"
# and time the sync so we know how much the kernel was hiding from us
time sync
Once everything is done I like for my computer to emit a sound so I know that it has completed it’s task and I should check in on it
#beeper
ogg123 /usr/share/sounds/KDE-Im-Irc-Event.ogg
And there you have it. The mighty vv
please let me know if you have suggestions for improvements by commenting here or making an issue on it’s github page
here it is in it’s entirety at the moment
#!/bin/bash
# this is a wrapper script that shows
# time info and beeps after said thing is done
# and nice and ionice for niceness
/usr/bin/time -v nice ionice -c3 "$@"
#beeper
time sync
ogg123 /usr/share/sounds/KDE-Im-Irc-Event.ogg