3

I'm creating a server on my RPi that's on a timer, so I want to have a command run at startup to begin the server processes. I've seen this: Execute script on start-up, but it doesn't explain it well enough, since I don't really know what kind of script it is. (P.S. I'm talking about the middle method's tutorial, just to clarify.) All I want to do is to run $ sudo /opt/jdk1.8.0/bin/java -Xms256M -Xmx496M -jar /home/pi/spigot.jar nogui (Tutorial Link) so it begins my server without me typing it in every day when I have better things to do than to SSH into my RPi so my friends can play Minecraft.

What script would I need to write? I understand everything else in the tutorial, but I don't understand how to write the code since I don't know what language it is. I am a fairly experienced coder, but the RPi is all that I have had to do with Linux, besides the occasional Ubuntu Wubi.

I will accept either code excerpts or a simple tutorial on actually writing the script. Post comments for any questions you have. I'm using a updated version of Debian, but without LXDE enabled on boot and 16 MB GPU RAM (headless server connected via SSH).

SlySven
  • 3,631
  • 1
  • 20
  • 46
Anonymous Penguin
  • 303
  • 1
  • 4
  • 19

3 Answers3

6

I assume you want the script to run on boot, but without having to log in.

There are many ways to do this, but you could just put your code into rc.local NOTE You do not need sudo, as the startup code is running as root.

You cannot edit etc/rc.local directly as it is owned by root; make a copy to edit and replace the original (with the correct permissions).

Only commands which can be run without login will work, of course.

Milliways
  • 62,573
  • 32
  • 113
  • 225
2

As an alternative (which I find easier to use and maintain) you can use cron. While mostly known for executing stuff at intervals or at specific times, it also has the possibility to do stuff after reboot. Add a line like this:

crontab -e
[...]
@reboot wait 5;/root/myscript.sh &

and cron will execute the command after each reboot. Waiting some seconds (as suggested by the first command "wait 5") might be necessary if your script depends on other stuff that takes some time to become available after the reboot, e.g. network integration, time synchronisation, or alike.

anon
  • 31
  • 1
0

I'd like to suggest that a study of the systemd environment is also a great candidate. This is the environment that is already present in Raspbian and is responsible for starting the majority of system demons at boot time. It takes a little study and work to get going but the results can be pretty good ... plus it gives the you the ability to start, stop, refresh and see status using the systemctl command.

A google search on systemd will provide a good start on reference materials.

A video tutorial on this technique is available here:

https://www.youtube.com/watch?v=eEuViHanjKI

And a write-up in a FREE Raspberry Pi book is available here:

https://leanpub.com/pi

Kolban
  • 1,794
  • 2
  • 16
  • 28