How can I launch mono application on startup with root rights?

5,455

Since you are running Ubuntu 16.04 which by default uses systemd you should write a systemd service unit to control the startup behaviour of your application.

A simple systemd unit file looks like as follows, assuming mono is at /usr/bin/mono since the ExecStart line must begin with an absolute path.
Save this in /etc/systemd/system/my-mono-app.service.

[Unit]
Description=my mono app
# If the service relies on network uncomment the next line.
#After=network.target

[Service]
Type=Simple
ExecStart=/usr/bin/mono /home/yahniukov/Documents/programs/my_programs/game_backup/backup.exe <your-parameters>

[Install]
WantedBy=multi-user.target

You have added "$@" which implies that there are some parameters passed to your command, so you have to replace <your-parameters> with the ones actually used.
You also could use the systemd options Environment or EnvironmentFile to store this. For more information have a look in the man pages systemd.unit, systemd.service and systemd.exec.

After you have created the file, execute the command as follows to tell systemd that there was a change.

systemctl daemon-reload

Now you should be able to control the service as follows. For a full description see man systemctl.

systemctl status my-mono-app
systemctl start my-mono-app
systemctl stop my-mono-app
systemctl restart my-mono-app

To enable the service as start up, you have to enter the command as follows.

systemctl enable my-mono-app

Depending on the behavior of your script and application, it might be necessary to change the Type=simple part to oneshot or forking. Not that you have to run systemctl daemon-reload after you have made changes to the my-mono-app.service file to pick up the changes.

Share:
5,455

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    Recently I wrote the program using Mono. This program needs the root rights and I want it launch on startup.

    I have Ubuntu 16.04.

    I already tried use "/etc/rc.local" (followed this link and this)

    /etc/rc.local:

    #!/bin/sh -e
    sudo game_backup.sh "$@" &
    
    exit 0
    

    /usr/local/bin/game_backup.sh:

    #!/bin/bash
    cd /home/yahniukov/Documents/programs/my_programs/game_backup
    sudo mono ./backup.exe "$@"
    

    Permissions of rc.local

  • Luidgi Gromat
    Luidgi Gromat almost 6 years
    In addition of Thomas answer, if your app use relative path you should add after ExecStart line : WorkingDirectory={path of your exe}