# Script and cron help needed



## silkstone (Jul 29, 2018)

Hi all,
I've been playing around with ubuntu server 16.04, scripts and cron for about 3 hours to no avail and need some serious help. It's rather frustrating as I managed to get what I wanted to do to work on a previous install, I just can't do it any more.

Basically, I have a minecraft spigot server running on an old laptop for my kids and i'd like to have it start in a new screen session on every reboot.

The commands I use to start the server are as follows:

Screen
cd minecraft
java -Xms512M -Xmx1008M -jar ./spigot-1.12.2.jar nogui

And the server starts fine.

I'm trying to make a script that i can put in cron to run @Reboot, but every attempt ends in failure.

I either get thousands of screen sessions open, an eula error (the text file needs to be set to eula=true (which it is already), or it just fails to run.
I've managed to make a script that will start the server (though I've since deleted it), I just can't for the life of me get it to work with cron and screen.

I'd like to run it in a screen session, rather than as a service so I can switch to the session and enter commands if needed.

If anyone could help, I'd greatly appreciate it.


----------



## re0 (Jul 29, 2018)

I haven't got my problem solving head on at the moment, but perhaps you can show us what your cron looks like if you currently have one set?

I understand that the following does not answer your question, but I propose that perhaps you should look at using Linux Game Server Manager (LGSM) if you're not so much into the nitty gritty of Linux since it's really easy to deploy and maintain. Plus, there is a lot of user-friendly documentation, including on how to use cronjobs to monitor, restart and update the server here for LGSM.


----------



## Aquinus (Jul 29, 2018)

You don't want to use cron for something that you want to start at boot, you need an init script. You also won't need screen if it's running as a service via said init script.


silkstone said:


> I'd like to run it in a screen session, rather than as a service so I can switch to the session and enter commands if needed.


There is a probably a better way to achieve that than running the process in screen or tmux.

Edit: See the "start-stop-daemon" command.


----------



## re0 (Jul 29, 2018)

^ This

Cron is probably not most suitable for the task as-is. It's better to stick to using cronjobs for scheduling tasks at regular intervals.


----------



## silkstone (Jul 29, 2018)

But, if I use it as a service, I won't be able to access the server commands, right?

I thought it'd be easy to start a screen on boot, run a command in screen, then detach as I had it set up before. I just can't for the life of me figure it out any more.


----------



## Easy Rhino (Aug 1, 2018)

You could create an init.d or systemd script that fires on boot that creates the screen session and launches minecraft. it shouldn't take much time reading the systemd docs.

https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux


----------



## Aquinus (Aug 1, 2018)

Using screen or tmux to run a service feels wrong to me, even if it's turned into a service by whatever your flavor of Linux does. If the OP wants to be able to execute commands on the server essentially remotely, I would see if there is a better rcon option for Minecraft so the need for being able to connect into the console is removed as a requirement. There has to be a server monitor thing that you can use to interact with the Minecraft server via a remote console without actually needing the console provided by the server itself.


----------



## silkstone (Aug 2, 2018)

Easy Rhino said:


> You could create an init.d or systemd script that fires on boot that creates the screen session and launches minecraft. it shouldn't take much time reading the systemd docs.
> 
> https://linuxconfig.org/how-to-automatically-execute-shell-script-at-startup-boot-on-systemd-linux



I did attempt to do that, but I just can't get my head around the coding without step-by-step instructions. My level of understanding of Linux is rather rudimentary. I can understand how to install packages, start services, hell, I even have set up a personal cloud and plex servers along with VPNs, DNS services and secure SSH and SFTP services linux computers with one having (fake) raid for redundancy. I can create basic scripts through copy-paste, but every attempt I make to modify a script, just ends in failure. I just can't get my head around how linux/ubuntu handles start up programs or how to properly code scripts.

I swear that all I did in the past was to do an @Reboot screen -d java . . . .  command in cron, but now I can't do that.

I think that I will continue to run the commands manually for now, and then just have it run as a service later. Console access isn't essential, it's just nice to have as it provides a quick and easy way to op, whitelist, blacklist as required if I open the server up to WAN.

Thanks for the help anyway. I'll try to continue with more experimentation and see how it works.



Aquinus said:


> Using screen or tmux to run a service feels wrong to me, even if it's turned into a service by whatever your flavor of Linux does. If the OP wants to be able to execute commands on the server essentially remotely, I would see if there is a better rcon option for Minecraft so the need for being able to connect into the console is removed as a requirement. There has to be a server monitor thing that you can use to interact with the Minecraft server via a remote console without actually needing the console provided by the server itself.



Lots of people have said it is wrong to run a screen on startup, but no one has been able to tell me why. Running it in screen always seemed to be the most simple option to get full console access. I had it working great a while ago on a raspberry PI, but since switching the server to ubuntu, It just doesn't work.


----------



## Easy Rhino (Aug 2, 2018)

silkstone said:


> I did attempt to do that, but I just can't get my head around the coding without step-by-step instructions. My level of understanding of Linux is rather rudimentary. I can understand how to install packages, start services, hell, I even have set up a personal cloud and plex servers along with VPNs, DNS services and secure SSH and SFTP services linux computers with one having (fake) raid for redundancy. I can create basic scripts through copy-paste, but every attempt I make to modify a script, just ends in failure. I just can't get my head around how linux/ubuntu handles start up programs or how to properly code scripts.
> 
> I swear that all I did in the past was to do an @Reboot screen -d java . . . .  command in cron, but now I can't do that.
> 
> ...



I think the point is that the best way to accompish what you want is to use an actual agent to login to your server and run commands. I do believe there are a few free ones out there. That way you can simply have minecraft start on boot and then fire up your client and run commands for days.


----------



## Aquinus (Aug 2, 2018)

silkstone said:


> Lots of people have said it is wrong to run a screen on startup, but no one has been able to tell me why. Running it in screen always seemed to be the most simple option to get full console access. I had it working great a while ago on a raspberry PI, but since switching the server to ubuntu, It just doesn't work.


It's not good practice because it's not just running the service, it's a service (screen or tmux,) running a shell, that's running a service (Minecraft.) The main issue is that it's not hard to start another shell with screen and that if Minecraft is running privileged (as root,) then it becomes a security concern. Root is only as secure as the weakest link and having something like screen running that a non-privileged user can gain access to makes root as a whole insecure. Running screen or tmux is a valid strategy for some kinds of problems but, every time it's not for long running services. There are other benefits to using a init.d script or upstart or something to start Minecraft. There is value in being able to do thing like "sudo invoke.rd minecraft-server [status|stop|start]" without having to enter a screen session to figure it out.

I'm not saying you can't use screen or tmux, you certainly can and it will definitely work. It's just bad practice in my honest opinion.


----------



## silkstone (Aug 2, 2018)

Ah. I never realized that it would be a security issue. I'll look into how to run it as a service a little further.


----------

