Write Down
Write Down

Reputation: 156

Linux run at start

I'm trying to start some processes when my raspberry pi boots. But for some reason it doesn't work as expected. The idea is to start Xvfb and some jar file.

So, I created a script

Xvfb :99 > /var/log/xvfb.log 2>&1
java -jar /home/pi/selenium-server/selenium-server-standalone-2.42.2.jar > /var/log/selenium.log 2>&1

echo "Servers started" >> /var/log/start_server.log
date >> /var/log/start_server.log

And then with a cronjob I start this script: @reboot /var/bootscripts/start_servers

But for some reason, the jar file isn't 'started' at all. Is it because I sent everything to log files?

Update

Okay, I found some information about writing a start script, so I modified my script.

#! /bin/sh
# /etc/init.d/blah
#

# Run always
> /var/log/xvfb.log                     # clear log
date >> /var/log/test-server.log        # write date to log

> /var/log/xvfb.log                     # clear log
date >> /var/log/xvfb.log               # write date to log
Xvfb :99 > /var/log/xvfb.log 2>&1       # write output to log

> /var/log/selenium.log                 # clear log
date >> /var/log/selenium.log           # write date to log
java -jar /home/pi/selenium-server/selenium-server-standalone-2.42.2.jar > /var/log/seleni$

echo "Servers started" >> /var/log/test-server.log

# Carry out specific functions when asked to by the system
# to do
case "$1" in
  start)
    echo "Starting script blah "
    echo "Could do more here"
    ;;
  stop)
    echo "Stopping script blah"
    echo "Could do more here"
    ;;
  *)
    echo "Usage: /etc/init.d/blah {start|stop}"
    exit 1
    ;;
esac

exit 0

It is not completly finished (# to do), but I first wanted to test the commands that should run always. Well, xvfb starts (log is perfect), but the java server doesn't. The selenium.log file isn't created (so it doesn't reach that piece of code I suppose).

If I see to test-server.log I only see the date, so the echo command doesn't work as well, or better said: it doesn't reach the command.

Upvotes: 1

Views: 585

Answers (3)

Write Down
Write Down

Reputation: 156

I solved the issue, I shared it over here: http://pastebin.com/PsBawgqK

To run at boot, I added the command to /etc/rc.local. But first I made it available globally: cp test-server /usr/bin/

Hope this will help some people.

Upvotes: 0

fquinner
fquinner

Reputation: 538

In general, when writing a cron job, I usually adopt this approach:

  1. Does the exact command line that I want to put in the cron work on the command line verbatim?
  2. If yes, create a cron that executes within the next minute or two, modify the cron, then wait until the time elapses and tail the cron log to ensure it runs (also obviously check crond is running)
  3. If that works, set cron to use the right time, otherwise debug your script (most likely the script's environment / JAVA_HOME stuff).

This may well be the best article I have ever read on the matter of cron jobs... I don't keep many bookmarks in my browser but this is one of them.

http://www.alleft.com/sysadmin/common-cron-mistakes/

Most of the problems I hit when writing crons are covered in there. It's the kind of thing you rarely get right first time so it does usually take a few iterations to get it right.

Upvotes: 0

Zan Lynx
Zan Lynx

Reputation: 54325

You have very likely run into a common problem with cron jobs and init scripts: the environment variables are not set to what you expect.

I advise setting the variables explicitly. Set the PATH and HOME values. If selenium server needs a GUI then after you start Xvfb you need to set DISPLAY and perhaps XAUTHORITY. You might need USER as well.

Another comment on that: I don't recommend doing this with a cron job, although it'll work. It would be better to do this with a SysV init script, or Upstart job or systemd unit, depending on what your Pi is running.

Upvotes: 1

Related Questions