Showing posts with label Cron. Show all posts
Showing posts with label Cron. Show all posts

Sunday, May 21, 2017

Cron: Copying all Labels from Blog to a File (Example)

Command:

$ cat smashingtheory_label_urls.sh


Result:

if [ $# -eq 1 ]; then
curl -s https://smashingtheory.blogspot.jp | sed -n "s/.*dir='ltr' href='https:\/\/smashingtheory.blogspot.jp\/search\/label\/\\([^']*\\).*/\1/p" > $1
else
echo "Usage: smashingtheory_label_urls.sh FILENAME" >&2
fi


Note:

Double backslashes before the round brackets should be single backslash. I did this to escape Mathjax's behavior.

Saturday, May 20, 2017

Crontab -e Error

To fix

Command:

$ export EDITOR=/usr/bin/vim
$ crontab -e

Tuesday, September 6, 2016

Crontab: Tweet Random Line without Modulo Bias

Command:

$ crontab -l


Result:

*/30 * * * * perl -le 'sleep rand 360' && csv="list.csv"; wc=$[`wc -l < $csv` - 1]; rand=$[`./rand_nobias.sh $wc`]; head -$[$[$rand \% $wc] + 2] $csv | tail -1 | /usr/local/bin/gawk -f formatcsv.awk | ruby tweet.rb 

Saturday, January 30, 2016

Debugging: Cron: % sign

Error message:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `]'
/bin/sh: -c: line 1: syntax error: unexpected end of file


The source code:

*/10 * * * * perl -le 'sleep rand 60' && head -$[${RANDOM} % `wc -l < /Users/username/lines.txt` + 1] /Users/username/lines.txt | tail -1 | ruby /Users/username/tweet.rb

Crontab man page:

The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

Fixed source code:

*/10 * * * * perl -le 'sleep rand 60' && head -$[${RANDOM} \% `wc -l < /Users/username/lines.txt` + 1] /Users/username/lines.txt | tail -1 | ruby /Users/username/tweet.rb

Friday, January 29, 2016

Cron: Tweet a Line Every 10-19 Minutes

Cron - Tweet a line every 30-40 mins:

*/10 * * * * perl -le 'sleep rand 540' && head -$[${RANDOM} \% `wc -l < /Users/username/lines.txt` + 1] /Users/username/lines.txt | tail -1 | ruby /Users/username/lines.rb

lines.rb - using standard input:

require 'json'
require 'oauth'

consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)

# STATUS
status = STDIN.gets

# POST
response = endpoint.post('https://api.twitter.com/1.1/statuses/update.json', status: status )
result = JSON.parse(response.body)