Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Twitter bot weatherlisbon

This little bash script shows how to use curl, grep, tail, sed and perl one-liners in order to compose a bleeding-edge twitter bot.
This one returns daily weather forecasts for Lisbon city based on the BBC weather forecast rss feed.

#! /bin/sh

#Goto here
here=/home/guillaume/Personal
cd $here

#BBC Lisbon weather id
id=0048

#BBC weather RSS feed address
feed="http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/${id}.xml"

#City
city=lisbon

#temporary file
file="weather${city}"

#Weather twitter bot
twitbot=weatherlisbon:*******

#Timestamp the log file
echo .>> $file.log
date >> $file.log

#Read the RSS feed and filter it
curl $feed | grep 'title' | tail -n 1 | perl -wlne'm/title>(.*)<\/title/i && print $1' | sed -e "s/&#xB0;//g" > $file.txt

#Read the forecast into a weather variable
read weather < $file.txt

#Twit the weather variable away
curl --basic --user $twitbot --data status="$weather" http://twitter.com/statuses/update.xml >> $file.log

Daemonize a Ruby process

// description of your code here
from : http://scie.nti.st/2006/12/15/daemonize-a-ruby-process
Here's a neat thing I found. I needed a small bit of Ruby code to run continuously in the background, but because of how Capistrano can't seem to work well with "nohup" and "&" (background job), my ruby script itself needed to be able to fork and detach from the terminal. Here's how you do it:
#!/usr/bin/ruby

pid = fork do
  Signal.trap('HUP', 'IGNORE') # Don't die upon logout

  loop do

    // Some code

    sleep 60
  end
end

Process.detach(pid)

Daemonize a Ruby process

Here's a neat thing I found. I needed a small bit of Ruby code to run continuously in the background, but because of how Capistrano can't seem to work well with "nohup" and "&" (background job), my ruby script itself needed to be able to fork and detach from the terminal. Here's how you do it:

#!/usr/bin/ruby

pid = fork do
  loop do

    // Some code

    sleep 1.minute
  end
end

Process.detach(pid)


In my case, I just needed some code to run each minute, but it is part of a Rails app, so I didn't just want to start it with cron every minute (Rails loading is expensive). Neat eh?

C - create Process Daemon

// Create a process Daemon

#include <stdio.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
	/*
	 * Funzione che mi crea un demone
	 */
	
	int pid;
	
	// create - fork 1
	if(fork()) return 0;

	// it separates the son from the father
	chdir("/");
	setsid();
	umask(0);

	// create - fork 2
	pid = fork();

	if(pid)
	{
		printf("Daemon: %d\n", pid);
		return 0;
	}
	
	/****** Codice da eseguire ********/	
	FILE *f;

	f=fopen("/tmp/coa.log", "w");
	
	while(1)
	{
		fprintf(f, "ciao\n");
		fflush(f);
		sleep(2);
	}
	/**********************************/
}

Python - create daemon

// Questa funzione permette di creare un demone in python

def createDaemon():
	'''Funzione che crea un demone per eseguire un determinato programma...'''
	
	import os
	
	# create - fork 1
	try:
		if os.fork() > 0: os._exit(0) # exit father...
	except OSError, error:
		print 'fork #1 failed: %d (%s)' % (error.errno, error.strerror)
		os._exit(1)

	# it separates the son from the father
	os.chdir('/')
	os.setsid()
	os.umask(0)

	# create - fork 2
	try:
		pid = os.fork()
		if pid > 0:
			print 'Daemon PID %d' % pid
			os._exit(0)
	except OSError, error:
		print 'fork #2 failed: %d (%s)' % (error.errno, error.strerror)
		os._exit(1)

	funzioneDemo() # function demo
	
def funzioneDemo():

	import time

	fd = open('/tmp/demone.log', 'w')
	while True:
		fd.write(time.ctime()+'\n')
		fd.flush()
		time.sleep(2)
	fd.close()
	
if __name__ == '__main__':

	createDaemon()
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS