Score:1

How can I log in and run a bash file at boot?

cn flag

I got myself a minecraft server that turns on and off on specific hours but I don't want to type the commands every time it should start playing and let it run those commands after boot. I found an old askubuntu thread (for 12.04) where this is handled and let it run as root. But for security reasons, I don't want it to run as root. I would like to still be able to type in commands to like stop the server, so it cannot run my bash script somewhere hidden.

waltinator avatar
it flag
Read `man 5 crontab`, and use the special `@reboot` date field.
bac0n avatar
cn flag
Sounds like you need an ordinary [startup](https://askubuntu.com/a/1089789/986805) script, this should work as a template, with `Group=`, `User=` [directive](https://manpages.ubuntu.com/manpages/focal/man5/systemd.exec.5.html) you may specify different user, group (I would also recommend `/usr/local/lib/systemd/system` instead of `/etc/systemd/system`).
Score:1
cn flag

You could try to use a crontab entry for your user.

The command to run a command att boot is "@reboot".

Example of crontab entry:

@reboot   cd /home && sh ./myscript.sh
Irsu85 avatar
cn flag
And... how can I set that crontab entry?
Level9 avatar
cn flag
Run the command `crontab -e`
Irsu85 avatar
cn flag
thanks, ill try that tomorrow
bac0n avatar
cn flag
Storing scripts in `/home` is not common (nor recommended). It's also preferable to use the full path to your command. Specifying the shell on the command line `/bin/sh script.sh` will pass the *interpret directive* `#!` as a regular comment instead of *specified* interpreter. And if they differ may lead to feature inconsistency.
Irsu85 avatar
cn flag
My script is also not in home, it's in /home/myUserName/survival
Irsu85 avatar
cn flag
This doesn't seem to work, but this gives me an idea of what i'm searching for. Thanks
Irsu85 avatar
cn flag
Update, I did some more research and it seems I used the command wrong...
Score:0
tr flag

To automatically run a command every time the system boots:

  • Search "Startup Applications" in the dash and open it.

  • Click add. For the name put "Start Minecraft Server" and under "command" put your command used to start your Minecraft server.

  • Press add and now the script should run on restart/startup.

Enjoy!

jp flag
Dan
If the Ubuntu installation used the server version instead of the desktop version, this will not possible as there's no way to access a GUI program. Although, for cases like these I would recommend a systemd unit as suggested by @bac0n comment's above. As you will most likely want easier control to stop and restart such software.
jackw11111 avatar
tr flag
Ah gotcha. I just thought I would share this method as a less programming-savy alternative.
jp flag
Dan
I didn't mean it as the answer is wrong, to complain or anything of the sort. Sorry if it sounded like that! The answer is absolutely fine. But I just wanted to mention it as a "disclaimer" for this specific case.
jackw11111 avatar
tr flag
@Dan All good, It was a good point I hadn't considered, thankyou! :)
Irsu85 avatar
cn flag
I like this idea, but I can't access a GUI.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.