Tuesday, August 20, 2013

Setting env variables in linux just for single command

Use this pattern:
FOOO=bar bash -c 'echo $FOOO'

Some working and non-working examples:

$ FOOO=bar bash -c 'echo $FOOO'
$ echo $FOOO

$ FOOO=bar echo $FOOO

Thursday, August 15, 2013

Jenkins + Multiple SCMs + git + hooks/triggers = mission Possible

Multiple SCMs plugin version 0.2 - in its documentation is remarked two controversial statements:
  • "Post-commit type triggers don't currently work".
  • "Allow polling to work with multiple instances" - in version 0.2
So is pooling possible? There are peaople on the internet stating the two opposite points. Yes, it is possible but some tricks are needed.
  • for every GIT repo, defined in Multiple SCM plugin config, an "Unique SCM name (optional)" is needed. 
  • check the version of your GIT plugin (Multiple SCM plugin uses GIT plugin). When my git plugin version was 1.1.x, the pooling did not work, and updating to git plugin 1.4 solved my issue.

Additional data:

Friday, August 9, 2013

linux: demonizing a process and killing it when you want

This is very useful for processes which do not write an PID file. This is extreme stupidness... but it could be work-arounded.

For example I want to start ant kill later the jasmine server, which is started this way:
bundle exec rake jasmine:server --trace

The whole batch script will be:
[ -f tmp/jasmine.pid ] && kill -9 `cat tmp/jasmine.pid` && rm tmp/jasmine.pid
bundle exec rake jasmine:server --trace &
echo $! > tmp/jasmine.pid
sleep 20
bundle exec rake jasmine:run
kill -9 `cat tmp/jasmine.pid`

rm tmp/jasmine.pid

My strings:
tmp/jasmine.pid - my pid file
bundle exec rake jasmine:server --trace - command to be deamonized
sleep 20 - this ensures time for the server to start
bundle exec rake jasmine:run - my task to be executed on the demonised process

Other information:
[-f file] && command - executes command if file exists
"echo $!" prints the pid of the last command in current console.

magic apostrophes in linux = Back Quotes for Command Substitution

They are replaced by the output of the command between them:

kill -9 `echo 9999`

is equivalent to:

kill -9 9999

killing app, listening on a defined port in single line

In Linux this will kill the process, listening on port 8888:

ps aux | grep port=8888 | awk '{print $2}' | tail -n 1 | xargs kill -9