I have a Raspberry Pi 2 with Raspbian. I am trying to start a Mono program called StartBrowser.exe automatically when the system boots. I added the following line to the /etc/rc.local file:
sudo mono /home/pi/Desktop/StartBrowser.exe
I also tried adding:
sudo /home/pi/Desktop/StartBrowser.exe
The program does not start on boot. When I run the same commands in the terminal the expected program starts. What do I need to do to start "StartBrowser.exe" on system boot ?
There are few steps to accomplish this:
Make sure your application can be run as Windows Service (check the documentation)
On RPi install mono-service with sudo apt-get install mono-4.0-service
Now you can add, at the end of the rc.local file, just before exit 0 line this call:
/usr/bin/mono-service -p:/directory/of/mono/app /directory/of/mono/app/app.exe
-p switch is mandatory, otherwise no additional DLLs will be loaded.
You can use "mono-service" to run programs in the background.
You can run your compiled code like so:
mono-service /home/pi/Desktop/StartBrowser.exe
By default, this creates a lockfile in /tmp. You can change this by using the -l: option.
In this way your service is running in the background
Related
I use Dev Containers to attach to a container and debug. And it was working just fine.
Recently however it shows this error when I hit F5 or run dotnet build from VS Code's terminal:
/usr/share/dotnet/sdk/7.0.102/NuGet.targets(132,5): error : Unable to obtain lock file access on '/tmp/NuGetScratch/lock/fcd2970c0c875310ff5855562ac5f3954170bddb' for operations on '/Crm/AdminApi/obj/project.nuget.cache'. This may mean that a different user or administrator is holding this lock and that this process does not have permission to access it. If no other process is currently performing an operation on this file it may mean that an earlier NuGet process crashed and left an inaccessible lock file, in this case removing the file '/tmp/NuGetScratch/lock/fcd2970c0c875310ff5855562ac5f3954170bddb' will allow NuGet to continue. [/Crm/AdminApi/Api.csproj]
And it shows the above message after trying to re-install every dependency, while NuGet has cached those dependencies already.
It works if:
1- I open a root bash using docker exec -it container_name bash and run dotnet build
2- I open a non root bash, and simply run sudo dotnet build
3- I open a VS Code termianl (which shows vscode as the user) and run sudo dotnet build
I tried sudo chown -R vscode:vscode /tmp/NuGetScratch/ as mentioned in dotnet error : Unable to obtain lock file access on '/tmp/NuGetScratch/lock/ and here, but that did not change anything. I then tried sudo chmod -R 777 /tmp/NuGetScratch and again no results. I even verified that the owner is changed using ls /tmp -lah | grep NuGet and this is the results:
drwxrwxrwx 1 vscode vscode 4.0K Feb 9 10:43 NuGetScratch
What else can I do?
This was recently logged as an issue: https://github.com/NuGet/Home/issues/12420
Possible workarounds listed from https://github.com/NuGet/Home/issues/12420#issuecomment-1423774814:
Run dotnet nuget locals temp -c to clear the /tmp/NuGetScratch (If there is a sticky bit in /tmp permissions then it won't work)
Set environment variable NUGET_ConcurrencyUtils_DeleteOnClose to 1 before running restore, so the lock files will be cleared after restore (This change is only available in NuGet 6.2 and above, probably .NET 7 preview6 and above)
Set environment variable NUGET_SCRATCH to a path. This variable will override the default temp folder. But it's only applied to NuGet version 6.2 and later, probably .NET 7 preview6 and above).
All the solutions I can find on this topic are very old and none of them appear to answer my question...
I am trying to create a windows service that can self update (or auto update by some external trigger). In the past, I had created a windows service that was installed with InstallShield and we were able to update auto update the service in a hacky way by making the service write a batch script to the local machine and then run the batch script, which would stop the service, overwrite the service executable and other files with the new ones, and restart the service. This surprisingly worked.
However, I have updated the service to use InstallUtil.exe and this auto update script no longer works... I assume it's something to do with the way InstallShield handles the service install vs how InstallUtil does it... but I can only make guesses as I don't fully understand what each is doing to the registry.
Since I can't just overwrite the files and restart the service with the InstallUtil method, I thought I'd write a batch script that runs sc.exe to stop the service, uninstall it entirely, write the new files, install the new service files, and then start it... unfortunately, I can't seem to get sc.exe to run from a windows service automatically because it requires admin permissions... I tried to force it to self-elevate to admin using this snippet, but it doesn't appear to work as a service (it works fine if I run it from command line not as a service)
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
Does anyone know how I can cause a windows service to self update? I can look into updating to a .NET Core Worker service if there is some method of self update in .NET Core that I'm unaware of... Any ideas are much appreciated... it really shouldn't be this hard to accomplish...
For reference, here is the batch script I am currently using (ignore odd variables and such as I am dynamically replacing some of them, it works great when launched manually, just doesn't work when the service tries to run it):
#echo off
setlocal enableextensions enabledelayedexpansion
::make sure to run whole script as admin (this restarts scripts as admin if not already in admin mode)
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
pushd %networkDirectory%
::stop running service
for /F "tokens=3 delims=: " %%H in ('sc query %serviceName% ^| findstr " STATE"') do (
if /I "%%H" NEQ "STOPPED" (
net stop %serviceName%
if errorlevel 1 goto :stop
)
::delete existing service after stopping
sc delete %serviceName%
)
:: install updated service files
set "releaseDir=%networkDirectory%\Release"
set "programFilesCopyDir=%ProgramFiles%\{_companyDirectory}\%serviceName%\Release"
:: copy service Release dir to local system program files
xcopy "%releaseDir%" "%programFilesCopyDir%" /S /Y /Q
::execute the install
pushd "%programFilesCopyDir%"
CALL %serviceName%.exe --install
::start service
sc start %serviceName%
For anyone else trying to accomplish this that stumbles on this... I ended up finding a solution. I use the same script posted in my question above, but I wrote code to set up a scheduled task with Windows Task Scheduler. The scheduled task runs the above script as a one time scheduled task. This works like a charm.
I used this NuGet package to write the Task Scheduler code I needed:
https://www.nuget.org/packages/TaskScheduler/2.8.20?_src=template
I'm using Ubuntu with the latest updates. I made a systemd service to run a script (Server.exe) (Mono, C#) when my server starts. Here's its configuration:
[Unit]
Description=My
After=network.target
[Service]
PIDFile=/home/my/server/bshserver.pid
WorkingDirectory=/home/my/server
ExecStart=/home/my/server/start
User=my
Group=my
SyslogIdentifier=My-Server
StandardOutput=syslog
Restart=on-failure
KillSignal=SIGINT
[Install]
WantedBy=multi-user.target
"start" file:
#!/bin/sh
echo Starting MyServer in /home/my/server
/home/my/server/Server.exe
exit $?
I was to try another Ubuntu, fail too
When I run the script /home/my/server/start normally (simply /home/my/server/start on my terminal), it is working as expected. Top reports it's using between 2 and, say, 5% of my CPU, which is normal.
But when I start it with service start, htop says it's always using at least 100% of my CPU (one core)
What could explain such a big difference in CPU usage?
There are a couple issues I'm seeing:
In order to start a mono application it must be run with the command mono. Use which mono to find the full path of mono installed on your server. As it is your script will report a failure every time its run.
Your systemd configuration has OnRestart=on-failure set, which is working more effectively than intended. Since your script will always fail systemd will continuously try to restart the service.
Update your script to include calling mono:
#!/bin/sh
echo Starting MyServer in /home/my/server
/user/bin/mono /home/my/server/Server.exe
exit $?
If your service uses ServiceBase then use:
#!/bin/sh
echo Starting MyServer in /home/my/server
/user/bin/mono-service /home/my/server/Server.exe
exit $?
Your script may imply that Server.exe will fork. Add Type=forking to your configuration.
[Unit]
Description=My
After=network.target
[Service]
Type=forking
PIDFile=/home/my/server/bshserver.pid
WorkingDirectory=/home/my/server
ExecStart=/home/my/server/start
User=my
Group=my
SyslogIdentifier=My-Server
StandardOutput=syslog
Restart=on-failure
KillSignal=SIGINT
[Install]
WantedBy=multi-user.target
I'm trying to learn some C# and am currently using the internal console for outputs, but when it comes to keyboard inputs, I've read that it can not be done in the internal console on VS 2017 for Mac.
So I try to do it on external console, but all I get is this :
bash -c 'clear; cd "/Users/gb/Projects/reTest/reTest/bin/Debug";
"/Library/Frameworks/Mono.framework/Versions/5.8.1/bin/mono32"
--debug --debugger-agent=transport=dt_socket,address=127.0.0.1:56795 "/Users/gb/Projects/reTest/reTest/bin/Debug/reTest.exe" ; echo $? >
/var/folders/s_/sljf42_d01bdxlb5s_rwgsj80000gn/T/tmp563f24ea.tmp;
echo; read -p "Press any key to continue..." -n1; exit'; exit
I guess the console tries to execute a .exe application which is not possible in this case !
It's a console project by the way... I haven't found any solution for that so far.
Thks.
After further researches I found out that my bash was kind of corrupted somehow so I modified the .bash_profile file which had some extra inconvenient text..
I am not sure if this is the right place to ask, but here it is: I created an ASP.NET Core web application, and copied all the files on my Ubuntu 14.04 server. I can compile and run without a problem, but now I want this application/web site to run permanently.
I followed all the steps described here https://docs.asp.net/en/latest/publishing/linuxproduction.html, installed nginx as reverse proxy to run with apache, and all of this run perfectly well.
BUT, trying to use supervisor and start the app from it, I systematically get an error /usr/bin/dotnet cannot execute binary file. But, if I move to be in the directory where the application is published, and manually type dotnet appname.dll it does start without a glitch.
I am not sure where to look to get this to work with supervisor. Thanks for your help (and if this question should be somewhere else, let me know)
I finally solved my problem, replacing the equivalent of the line command=bash /usr/bin/dotnet /var/aspnetcore/HelloMVC/HelloMVC.dll as described in https://docs.asp.net/en/latest/publishing/linuxproduction.html under "Configuring Supervisor" by a little script, far from perfect as I get a Warning: HOME environment variable not set.
Anyway, here is the script:
#!/bin/bash
cd /var/aspnetcore/foesuivi/
dotnet FoESuivi.dll
cd $HOME
As a windows programmer, I don't know much about bash scripting, but I certainly can see that I would need to give a value to $HOME before the cd command.
Anyway, after doing a chmod +x to the sh file, and replacing the command= with the full name of the sh file, it is now working, I can reboot and my site is immediately available.