Linux – Automatically starting C# Mono application on Ubuntu server

linuxmonoshell

I have a TCP server written in C# which I will run on my server. I've tested the application running on the server and client side and it all works great! I'm using mono on my Linux server to run the Server.exe application. I've been experimenting with both Upstart and Shell scripts to get the Server.exe application to run in the background at start up. None of these have worked.

Here is the Shell Script I tried:

#!/bin/sh

/usr/bin/mono Server.exe "$@" &

I then edited the rc.local file found in the /etc directory and added this line:

/usr/local/bin/ClipCloud/start.sh &

Am I missing something here as I can't seem to get it working. It seems to start and then terminate the process straight away. When I start the Server.exe application from terminal by just typing

mono /usr/local/bin/ClipCloud/Server.exe

it works fine and will continue to run until I close the terminal window or type "exit"

I will be happy to share anything I can to help.

Best Answer

Me too was having this problem. I am now able to run my mono c# app on start up with the help of: http://www.stuffaboutcode.com/2012/06/raspberry-pi-run-program-at-start-up.html

In the "start" section of file (as specified in the above link), do the following: 1. change directory to the folder of your c# app: cd /home/pi/myApp 2. in the next line, add full path of "mono" AND full path of your c# app. For example: /usr/bin/mono /home/pi/myApp/myApp.exe

in the "stop" section, I am not sure how to stop the exact "myApp.exe"; but i stopped the "mono" by giving command:

killall mono

(in effect, the myApp.exe... but if you are running more than one c# apps..please test it.)

Notes

  1. make sure your c# app don't asks for user input...otherwise it will hang
  2. Don't forget to run sudo update-rc.d NameOfYourScript defaults :)
  3. test the script by running "start" and "stop" commands (as specified in the above given link) before you restart your machine.
  4. ALL comment lines in the script is a must. Otherwise you won't be able add it to start up list.

All the best! :)