How to play a video from a Windows service in Python - python

I use the technique described in this post:
Is it possible to run a Python script as a service in Windows? If possible, how?
to make a Windows service in Python. I starts OSC and waits for a message to start a video. The thing is, I can't make it start a video. At any stage. I used subprocess.call, I used os.startfile and I used win32api.WinExec. I put the command in a .bat file and started that. Nothing worked. Is it impossible?
I changed the script to start the video when the service starts, so this is not an OSC problem.

Related

Application on Raspberry Pi (Linux) with OpenCV that autostarts

I have used OpenCv within my Windows applications in the past and in this case, an application would be built and installed as a Windows Service so that it could be set to start automatically and start running. Differences are I have done these in compiled languages and we were on Windows.
Now, I am playing around with porting the application to run on Linux/Raspberry Pi. The application simply gets a video feed, does some object detection using OpenCv and then sends result via HTTP web api.
First comment before my question is (I am still getting familiar with this setup) it seems that Python is by far the language of choice for all of this. However, the end goal is to have this device be headless (no monitor or input devices and act like an IoT device) so I don't need or better, can't open a console and type commands.
So, for the question, what is the equivalent to a Windows Service on Raspberry Pi so that my application just starts up on boot and runs as long as the device is on? The subjective follow up question is Python still a good choice considering everything I have described above or would I be better off doing a full blown compiled app in c or c++?
Thanks!
If you are using Raspbian, then I would say the easiest tool il systemd (daemon) and the systemctl (shell command).
In order to run your python script as a daemon (a daemon is what Windows calls "Service") is to create a configuration file named .service and put it in the /etc/systemd/system path.
To get an idea of how to configure the file, you can take this example:
[Unit]
Description=Your service name
[Service]
ExecStart=python <path to python script>
StandardOutput=null
[Install]
WantedBy=multi-user.target
Alias=this_script_name>.script
Hope it helps!
Check out Supervisor: http://supervisord.org/. It should do what you need to do in terms of running your program on boot and restarting if it crashes, etc.
I don't have any experience with OpenCV, but web app frameworks like Flask (http://flask.pocoo.org/) make it very easy to expose an HTTP API with minimal code.
Good luck!

How can I monitor a python scrypt and restart it in the event of a crash? (Windows)

I have a simple python script to send data from a Windows 7 box to a remote computer via SFTP. The script is set to continuously send a single file every 5 minutes. This all works fine but I'm worried about the off chance that the process stops or fails and the customer doesn't notice the data files have stopped coming in. I've found several ways to monitor python processes in a ubuntu/unix environment but nothing for Windows.
If there are no other mitigating factors in your design or requirements, my suggestion would be to simplify the script so that it doesn't do the polling; it simply sends the file when invoked, and use Windows Scheduler to invoke the script on whatever schedule you need. By relying on a core Windows service, you can factor that complexity out of your script.
You can check out restartme the following link shows how you can use it
http://www.howtogeek.com/130665/quickly-and-automatically-restart-a-windows-program-when-it-crashes/

Keeping a program going on an external server

I have an external server that I can SSH into. It runs bots on reddit.
Whenever I close the terminal window the bot is running in, the process stops, which means the bot stops as well.
I've tried using
nohup python mybot.py
but it doesn't work - when I close the window and check the processes (ps -e), python does not show up. Are there any alternatives to nohup? Ideally, that print the output to the terminal, instead of an external file.
Have you considered using tmux/screen? They have lots of features and can help you detach a terminal and re-attach to it at a later date without disrupting the running process.

How to launch a python process in Windows SYSTEM account

I am writing a test application in python and to test some particular scenario, I need to launch my python child process in windows SYSTEM account.
I can do this by creating exe from my python script and then use that while creating windows service. But this option is not good for me because in future if I change anything in my python script then I have to regenerate exe every-time.
If anybody have any better idea about how to do this then please let me know.
Bishnu
Create a service that runs permanently.
Arrange for the service to have an IPC communications channel.
From your desktop python code, send messages to the service down that IPC channel. These messages specify the action to be taken by the service.
The service receives the message and performs the action. That is, executes the python code that the sender requests.
This allows you to decouple the service from the python code that it executes and so allows you to avoid repeatedly re-installing a service.
If you don't want to run in a service then you can use CreateProcessAsUser or similar APIs.
You could also use Windows Task Scheduler, it can run a script under SYSTEM account and its interface is easy (if you do not test too often :-) )
To run a file with system account privileges, you can use psexec. Download this :
Sysinternals
Then you may use :
os.system
or
subprocess.call
And execute:
PSEXEC -i -s -d CMD "path\to\yourfile"
Just came across this one - I know, a bit late, but anyway. I encountered a similar situation and I solved it with NSSM (Non_Sucking Service Manager). Basically, this program enables you to start any executable as a service, which I did with my Python executable and gave it the Python script I was testing on as a parameter.
So I could run the service and edit the script however I wanted. I just had to restart the service when I made any changes to the script.
One point for productive environments: Try not to rely on third party software like NSSM. You could also achieve this with the standard SC command (see this answer) or PowerShell (see this MS doc).

Run terminal command as startup reacts different from manually (linux raspberry pi)

Here's a short description of what I have:
I have to raspberry pi's in a local net work. I one of them I have a .py script named watchdog.py that starts a stream and then uses a sshpass command to the other pi to display the video stream.It also has some signaling LEDs a some push buttons for control
the problem is:
If I open a terminal and run the watchdog.py script in the GUI everything runs as it should be. So I thought of running it as a service as boot and installed upstart and made it run as a service (successfully I think). The thing is. If I boot the pi and then press the button to start the streams,they wont play on the other Pi, the LEDs ligh up and all the buttons work. And even the CPU load behaves the same way, but i still don't video nor audio. I have thought of trying automatically open a terminal (LXterminal) widow and run the python scrip on that window. but I didn't want the streaming raspberry pi also booting into gui (tough I guess I would mind if that makes the whole thing work).This little thing i making the whole project useless.
What are you using to play the streams? Depending on how you boot up the second Raspberry it might not have started some daemons for audio/video playback?!
You should (if you're not already doing) write a log (import logging ;)) and write a logfile which you can track for errors.
answer moved from OP's question itself:
I found a way that seems to work so far. instead of running the python script as a service I tried running it as cron job at reboot, and it worked. now it all works straight from reboot and I have Audio and video.

Categories

Resources