xdg-open: no method available for opening ./directory - python

I try to open a directory using xdg-open in Ubuntu. It works if I run xdg-open ./dir in terminal.
I have a Flask web app that opens directories using xdg-open in some situations. When I start the app from terminal in development mode (By running $ flask run ) it works and opens all directories without any problems. But when i start it in production mode using Nginx & Gunicorn it returns:
xdg-open: no method available for opening ./test
The result is exactly same as the situation that I run xdg-open in non-graphical terminal(alt+ctrl+f1)
What should I do?

Finally I found the solution
Just I must set the $DISPLAY env variable to :0 and then run the command.
s.th like this:
env = dict(os.environ)
env['DISPLAY'] = ":0"
subprocess.Popen('xdg-open ./some_folder',env=env,shell=True)

Related

Django waitress- How to run it in Daemon Mode

I've a django application with waitress (gunicorn doesn't work on windows) to serve it. Because its production code and its based on windows 2012 server. But I want the django application to run in daemon mode is it possible?
Daemon mode - app running without command prompt opening/visible also it'll be helpful to open the shell without closing the server. AutoStart if for some reason system has to restart.
Note:
Limitations: The project cannot be moved to UNIX based system.
Third-Party applications like any .exe file cannot be used.
You cannot use Docker as it consumes a lot of space.
For production:
create a file server.py at same level as manage.py and add following:
from waitress import serve
from myapp.wsgi import application
if __name__ == '__main__':
serve(application, port='8000')
Start-Process python -NoNewWindow -ArgumentList "server.py"
You can close the terminal after that and it still runs.
If you later want to stop, you have to do with Get-Process and then
TaskKill
Running with CMD:
START "myapp" /B python server.py
Running in cmd
Just add & at the end of command
Another solution - to use Docker, and in your docker you can use gunicorn or any other linux feature

How to keep AWS Lightsail virtual server running [duplicate]

I have set up Flask on my Rapsberry Pi and I am using it for the sole purpose of acting as a server for an xml file which I created with a Python script to pass data to an iPad app (iRule).
My RPI is set up as headless and my access is with Windows 10 using PuTTY, WinSCP and TightVNC Viewer.
I run the server by opening a terminal window and the following command:
sudo python app1c.py
This sets up the server and I can access my xml file quite well. However, when I turn off the Windows machine and the PuTTY session, the Flask server shuts down!
How can I set it up so that the Flask server continues even when the Windows machine is turned off?
I read in the Flask documentation:
While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well and by default serves only one request at a time.
Then they go on to give examples of how to deploy your Flask application to a WSGI server! Is this necessary given the simple application I am dealing with?
Use:
$ sudo nohup python app1c.py > log.txt 2>&1 &
nohup allows to run command/process or shell script that can continue running in the background after you log out from a shell.
> log.txt: it forword the output to this file.
2>&1: move all the stderr to stdout.
The final & allows you to run a command/process in background on the current shell.
Install Node package forever at here https://www.npmjs.com/package/forever
Then use
forever start -c python your_script.py
to start your script in the background. Later you can use
forever stop your_script.py
to stop the script
You have multiple options:
Easy: deattach the process with &, for example:
$ sudo python app1c.py &
Medium: install tmux with apt-get install tmux
launch tmux and start your app as before and detach with CTRL+B.
Complexer:
Read run your flask script with a wsgi server - uwsgi, gunicorn, nginx.
Been stressing lately so I decide to go deep.
pm2 start app.py --interpreter python3
Use PM2 for things like this. I also use it for NodeJs app and a Python app on a single server.
Use:
$sudo python app1c.py >> log.txt 2>&1 &
">> log.txt" pushes all your stdout inside the log.txt file (You may check the application logs in it)
"2>&1" pushes all the stderr inside the log.txt file (This would push all the error logs inside log.txt)
"&" at the end makes it run in the background.
You would get the process id immediately after executing this command with which you can monitor or verify it.
$sudo ps -ef | grep <process-id>
Hope it helps..!!
You can always use nohup to run any scripts as background process.
nohup python script.py
This will run your script in background and also have its logs appended in nohup.out file which will be located in the directory script.py is store.
Make sure, you close the terminal and not press Ctrl + C. This will allow it to run in background even when you log out.
To stop it from running , ssh in to the pi again and run ps -ef |grep nohup and kill -9 XXXXX
where XXXX is the pid you will get ps command.
I've always found a detached screen process to be best for use cases such as these.
Run:
screen -m -d sudo python app1c.py
I was trying to run my flask app for testing in my GitHub CI and the step where I was running the app was getting stuck for ever. The reason was that it was never releasing the command line
Best solution I found was a combination of two other responses in here:
nohup python script.py &

Django, how to run a command through Windows Task Scheduler

How to schedule and run a specific command in django using Windows Task Scheduler. My django project is not currently local server deployed but using the manual set up just like activating the virtual environment and then typing the python manage.py runserver on terminal rather deploying through xampp or laragon. But i am bit confused on how to achieve to schedule and run a command like python manage py get_source through the use of Windows Task Scheduler.
Don't know if you're still looking for this but I found a working solution here - configure .bat file to run commands in Django Virtual Env
Very simple; point to the location of your project directory...
cd C:\webapps\my-project-dir-with-manage.py-inside
copy and paste the contents of the system generated activate.bat,found inside your venv/Scripts folder...
add your command line...
.\manage.py <your command here>
save as myfile.bat, and schedule via the Windows Task Scheduler.
Super simple.
I had the same problem.
The main issue is, that you missed the full path to python.exe as an execution application. "Python" will not work.
And then your application as an argument.
Additionally, to that, you can add w to py. This will make it runnable on windows without a .bat file.
Application to execute:
C:\user\python.exe Argument: manage.pyw runserver

ssh running a command gives different results to running it locally

I have a python script that uses Popen to create an appium server for a simulator on a mac
self.appium_process = subprocess.Popen(["/usr/local/bin/appium", "-a", self.ip, "--nodeconfig", self.node_file_path, "--relaxed-security", "-p", str(appium_port), "-dc", default_capabilities], stdout=log_file, stderr=subprocess.STDOUT)
I created a bash shell script that calls the python script. When I run the script from the local box it works and the appium logs show the connection.
I need to run this remote via ssh however. So I use the following to call the script:
ssh 10.18.66.99 automation_fw/config/testscript.sh
This, however, always ends up with the log showing:
env: node: No such file or directory
I checked and the node app has an extra slash before its called:
$ which node
/usr/local/bin//node
$
I tried changing the path on the machine but no change. How can I get this to run from ssh in the same way as it can run locally on that same box
A
When you are running a command vis SSH you are not starting what's called a login shell (more about that here).
From the details you've shared, I would say it's some thing in your environment (running outside a logged-in shell), more specifically a problem with your $PATH variable. You might want to check /etc/environment or similar paths (depending on your Linux flavour) for the wrong value.

Run DJango server from outside of virtual environment by using script

I have created DJango Project inside virtual environment.
Let's say virtual environment DJangoProject project name is Mysite
I am tired of running ./manage.py runserver everytime so that I wanted to automate running server when I was logged into ubuntu.I tried many ways but was always failing to access the path of virtual environment. So help me to run manage.py runserver from outside of virtual environment using bash or shell scripting.
any help can be appreciate!
What are looking for is something like Gunicorn, because runserver is only for development purposes. This is detailed tutorial for you to use
Link
You can't "run manage.py runserver from outside of virtual environment" if your project relies on a virtual environment... But nothing prevents you from writing a wrapper bash script that cd into your project's root, activate the virtualenv and launch the dev server.
This being said, I really don't see the point - and I even see a couple reasons to not do so, the first one being that you definitly want to keep the terminal where you run your dev server from opened so you can read all the logs in real time.
That's easy in Ubuntu you just need to create a shell script and run it from it's folder in the terminal as : ./shellscriptname.sh
but for that first you need to create the shell script by writing the same commands used on terminal to run the django server.
Example:
echo Hello Sangeeth
echo Lets start the app
cd myapp
cd venv
source bin/activate
cd ..
cd myapp
python manage.py runserver
even though this method helps you run the app , you wont be seeing the actual folder access and virtual environment on the terminal.

Categories