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).

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.

  • So would this work: do $ sudo nano etc/rc.local and place the command without sudo on a new line and then save it, reboot, and watch the magic happen? – Anonymous Penguin Sep 24 '13 at 0:52
  • 2
    @AnnonomusPerson why don't you try and see? =) – lenik Sep 24 '13 at 4:22
  • directly as it is owned by root; make a copy to edit and replace the original (with the correct permissions) How do I do that? I tried the command I was talking about and it was blank and it didn't save... could I somehow edit the SD card via Windows to change that file and add the code so it will work correctly? – Anonymous Penguin Sep 25 '13 at 1:52
  • 1
    Use sudo nano /etc/rc.local You really should read some basic texts on linux. I probably should have said /etc/rc.local but most would understand the filesystem, and immediately have realised the cause of the error. Incidentally, this is not the best way to modify system files. You really should only edit on a copy, and backup the original. This isn't really the best place for an introductory tutorial. – Milliways Sep 25 '13 at 4:34
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.

1

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

  • -1 Look at the date of the question - systemd was not present in the Debian distribution when this was asked... – SlySven Jun 5 '18 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.