/etc/rc.local is missing from my headless ubuntu 18.04 - bash

I am currently hosting a minecraft server on ubuntu server 18.04 LTS. I have a .sh script to start the server's java file, and I would like to run it at startup so that the minecraft server starts when the physical server boots. I wished to do this via /etc/rc.local. However, I do not see rc.local in that location.
Is it in a different location for this version of ubuntu, or is there an entirely different method I should use to run this .sh at startup?

There is no "/etc/rc.local" file on Ubuntu 18.04, but you can create it.
Create the file with a text editor:
sudo nano /etc/rc.local
Paste the following lines and replace "COMMANDS" with the commands to be executed at system startup:
#!/bin/sh -e
COMMANDS
exit 0
Add the execute permission on the file:
chmod +x /etc/rc.local

Set a crontab for this
Make sure the file is executable:
chmod +x /path_to_you_file/your_file/file.sh
To edit crontab file:
crontab -e
Then add this:
#reboot /path_to_you_file/your_file/file.sh

rc.local is disabled by default.
Enable by using this command
sudo systemctl enable rc-local.service

Related

How to automatically start your docker services on the restart of my computer?

I want to write a bash script file (.sh) file in Ubuntu so that docker services start automatically on reboot.
User the cron service.
You need to use special string called #reboot. It will run once, at startup after Linux reboots. The syntax is as follows:
#reboot /path/to/job
#reboot /path/to/shell.script
#reboot /path/to/command arg1 arg2
#So to run docker on reboot:
#reboot start docker
This is an easy way to give your users the ability to run a shell script or command at boot time without root access. First, run crontab command:
$ crontab -e
OR
# crontab -e -u UserName
# crontab -e -u vivek
So, to run a script called /home/vivek/bin/installnetkit.sh
#reboot /home/vivek/bin/installnetkit.sh

Editing crontab using a script

I am trying to write an embedded application upgrade script. I am running my application on Ubuntu on Beaglebone Black.
Right not, I am launching my application on power-up. To do this, I am running launch.sh script in crontab.
This script is as mentioned below:
until /root/aa_main; do
echo "Application aa_main crashed with exit code $?. Respawning.." >&2
echo "Crashed! $(date)" >> crashlog.txt
sudo sync
sudo reboot
done
It basically, reboots the system if my application crashes, and crontab launches the application again on reboot.
The problem is that if I want to update the application. I have to perform following steps:
Disable launching of the above script with crontab -e
Reboot
Copy and replace aa_main
Enable launching of the same script with crontab -e
Reboot
I want to automate this process. How can I do it?
Is it possible to edit crontab using a script?
Thanks in advance
You can use crontab command's other options to copy the crontab to a file, modify it and install the modify version from a script:
crontab -l would list the current crontab file, you can redirect this to a file of your choice and modify it
crontab <filename> would install the file specified by <filename> as the new crontab - use this option with your modified file and you'll get the new modified crontab

Run commands via Jenkins?

I have a script on remote Ubuntu server. I trying to execute the script after the jenkins build is succeeded, But the error says like this:
sudo: no tty present and no askpass program specified
The configuration is given below,
Can anyone help me?
Thank You.
The problem is that your script uses sudo at some point. The usual way around is to add the script that requires you to use sudo to the sudoers.
Example: in your script you use sudo service apache2 reload, now create a bash script containing that line and add that script to the sudoers file.
New script name: /home/quaser/restart-apache.sh
Use: visudo
Add at bottom of the file:
jenkins ALL=(ALL) NOPASSWD: /home/quaser/restart-apache.sh
Now, in your script change: sudo service apache restart to sudo /home/quaser/restart-apache.sh and you should not be asked for a password.
I had the same problem, I solved that by commenting Defaults requiretty on /etc/sudoers
cat /etc/sudoers| grep tty
#Defaults requiretty
From the man page:
man sudoers | grep requiretty -A 5
requiretty If set, sudo will only run when the user is logged in
to a real tty. When this flag is set, sudo can only be
run from a login session and not via other means such
as cron(8) or cgi-bin scripts. This flag is off by
default.

Shell script to login to ubuntu server and then process commands

I have a shell script(.sh file) which logs in to ubuntu server using ssh command. Once logged in i want to do the following commands:
sudo -i
su web
I want to incorporate the above commands in the same .sh file. How do i do it? Thanks

modify file content

I'm installing a lighttpd server on a remote machine using a bash script. After installation, I need to configure the port for the server. The system says I don't have permission to modify the file /etc/lighttpd/lighttpd.conf even though I do
sudo echo "server.bind=2000" >> /etc/lighttpd/lighttpd.conf
How shall I modify this?
What you're doing is running echo as root, then trying to append its output to the config file as the normal user.
What you want is sudo sh -c 'echo "server.bind=2000" >> /etc/lighttpd/lighttpd.conf'
Try to change the file permission using chmod
$ sudo chmod a+x /etc/lighttpd/lighttpd.conf
If you don't have the right to change the file /etc/lighttpd/lighttpd.conf check the man page of lighthttpd. If you can start it with a different config file, then create a config file somewhere and start lighthttpd with it.
The problem is that the bit on the right of >> is not run under sudo. Either use sudo -i to bring up a root shell a superuser and run the command, or just use an editor as mentioned before.

Resources