Ubuntu - Run Command on Start-Up with "Sudo"

Ubuntu - Run command on start-up with sudo

You can add the command in the /etc/rc.local script that is executed at the end of startup.

Write the command before exit 0. Anything written after exit 0 will never be executed.

Run Bash script as root in startup on ubuntu 18.04

Create the service file as in the template below and add the file in the location /etc/systemd/system/

And the Template as

[Unit]
Description = ~Name of the service~

[Service]
WorkingDirectory= ~directory of working file~
ExecStart= ~directory~/filename.sh

[Install]
WantedBy=multi-user.target

Start the service file by the name using

systemctl start servicefile.service

To enable on startup

systemctl enable servicefile.service

To check the status

systemctl status servicefile.service

To stop

systemctl stop servicefile.service

Ubuntu - Run command on start-up

Navigate to /etc/ and run

sudo nano rc.local

Place your commands below the comments, and above the command, "exit 0"

The commands should run on startup

How to sudo su; then run command

Unless you have an unusual setup, you can't normally string su with other preceding commands like that. I would imagine it is running sudo su, then hanging in the root environment/session, because it's waiting for you to exit before preceding to the pm2 commands. Instead, I would consider something along the lines of this using the -c option:

CMD="sudo su -c 'pm2 restart 0; pm2 restart 1'"
ssh -i somepemfile.pem ubuntu@1.1.1.1 "$CMD"

As suggested in another answer, it would also probably be useful to encapsulate the $CMD variable in quotes in the ssh call.

How to run a command on startup for ubuntu server 18.04?

I followed this document to successfully run a command at startup.

I put the script inside the user data. Here's my user data:

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/echo hello > /home/ubuntu/test.log
--//

/bin/echo hello > /home/ubuntu/test.log will be triggered on every boot. This is because SCRIPTS-USER is set to ALWAYS.

Run Terminal at startup and execute command as sudo

I found the solution, so I'm gonna post it here in case someone else needs it. If it already exists in somewhere else around here, feel free to tag it as duplicated.
The solution that worked for me was this:

sudo nano ~/.config/autostart/myprogram.desktop

[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=myprogram
Exec=lxterminal -e bash -c 'sudo /home/pi/myfolder/myprogram;$SHELL'
Terminal=true

sudo chmod a+r ~/.config/autostart/myprogram.desktop

The $SHELL makes the terminal stay open after myprogram ends its execution. If you don't need this feature, just exclude the ;$SHELL part of the code above.



Related Topics



Leave a reply



Submit