Skip to main content
Topic: Control #twatch from Linux command line and CRON job (Read 5222 times) previous topic - next topic

Control #twatch from Linux command line and CRON job

Hi!

I received my #twatch the other day. I found out that the most basic way to speak to the #twatch is using netcat (nc). Netcat probably comes as standard with most Linux distros and if it doesn't, then install it. On a Debian-flavour platform (like Ubuntu), this should do the trick:

Code: [Select]
sudo apt-get update
your password
sudo apt-get install netcat

Running the above even if you have netcat won't do any damage to your system. And if you have a different package management system, use that :)

Now that you have netcat, try this:
Code: [Select]
echo -n "Hello, world!" | nc -q 0 ip.address.of.your.twatch 1337 

The -n switch for echo makes it not send a newline after the text which would only look ugly on the #twatch. The -q 0 (that's a zero) switch for netcat means nc should exit after it's sent the input "Hello, World!" to your #twatch.

You can expand on the basic example with just a little flair:
Code: [Select]
echo -en "376130`date`" | nc -q 0 ip.address.of.your.twatch 1337

Note the -en switch for echo. It means that the funny stuff should be interpreted specially. In the case above it will send the bits needed to clear the screen. The `date` will expand to whatever the command date outputs, ie the current time and date. For some reason, the last two spaces on my #twatch are blank after this but it's not the end of the world. I can live without knowing which second my computer thinks it is, especially since it takes a few seconds for the #twatch to interpret and render the data sent to it.

For fun, i created a cron job (crontab -e) to keep my #twatch updated with the current time & date
Code: [Select]
* * * * *       echo -en "376130`date`" | nc -q 0 ip.address.of.your.twatch 1337  2>&1 > /dev/null

This should keep you occupied for some time :)

Re: Control #twatch from Linux command line and CRON job

Reply #1
Really nice and easy way to control the #twatch! Thanks for posting this. I split it to a new topic because I thought it deserved more attention :)
Got a question? Please ask in the forum for the fastest answers.

Re: Control #twatch from Linux command line and CRON job

Reply #2
I recently found out cron and it's really cool! I just have one question about your code: Any specific reasons for creating a shell script file and running it with cron?

Re: Control #twatch from Linux command line and CRON job

Reply #3
Often it's a lot cleaner to create a shell script instead of feeding cron with a giant one-liner. Also, you can run your script from the command line and when it works, add the script to cron.

Re: Control #twatch from Linux command line and CRON job

Reply #4
> Also, you can run your script from the command line and when it works, add the script to cron.

Beware: cron's environment is often very different to your user environment. It pays to hard code file paths to binaries etc.

Re: Control #twatch from Linux command line and CRON job

Reply #5
Indeed true, to a degree. If you look at Linux scripts, they often start with stuff like SOMEBINARY=/usr/local/bin/somebinary and then they run $SOMEBINARY later in the code. Makes your scripts a lot more maintainable.

So yes, use absolute paths in your Cron-invoked scripts, but do use variables to store those absolute paths.

I'd throw in something about The Path to Enlightenment here, but i can't find a really snappy way to put it :)