ffmpeg permission denied though the user has permission - python

I have installed ffmpeg in my server running ubuntu 14. my flask app project root is at /var/www/project/
i am able to upload files and my python scripts are able to write new files, but when i run ffmpeg in my script it gives permission denied.
Also running the below line in terminal
ffmpeg -i path/to/inputfile /path/to/outputfile & ps aux | grep ffmpeg
gives the user running the process is "ubuntu" which has write access to the project folder and its subfolders. As i told you other python script run by user "ubuntu" is able to create file.
permission denied error happens in both cases(python script and directly in terminal)
update:
also looking at the confusion with the permissions, is there a way i can run the app from /home/ubuntu/project instead of /var/www/project. i am using mod_wsgi with apache for my flask-app.

right now i have added sudo and it is working fine, though i am not sure running ffmpeg as sudo would cause any security issue. Anyone please clarify on this.
cmd='sudo ffmpeg -i '+f_name+' -acodec '+job['audio_ff']+' -b:a '+job['audio_bps']+'k - b:v '+job['video_bps']+'k -s '+job['res_width']+'x'+job['res_height']+' '+new_file
p=subprocess.Popen(shlex.split(cmd),stdout=subprocess.PIPE)

The files which are created by your app resides in the var/www/project and these need administrative privilege, if you want to read/access them. Without using sudo you cannot access the administrative protected files.
You can change the rights of the files created by your app to be accessed by ffmpeg without sudo or change the ownership to your username with chown..
Have you checked if your ffmpeg executables have permissions for all the users?

Related

How to use a python package with sudo privileges inside Flask?

I have a Flask setup in my Raspberry Pi 4 Model B via this tutorial.
OS = Ubuntu Server 20.04.2 LTS
Python = 3.8
Please keep in mind that I am using virtual env for my Flask application as shown in the tutorial and my Flask application is running absolutely fine.
Now I installed Adafruit_DHT in the same venv and tried using the following code in one of the endpoints
import Adafruit_DHT
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 24)
to which I am getting the following error
File "/usr/local/lib/python3.8/dist-packages/Adafruit_DHT/common.py", line 81, in read
return platform.read(sensor, pin)
File "/usr/local/lib/python3.8/dist-packages/Adafruit_DHT/Raspberry_Pi_2.py", line 34, in read
raise RuntimeError('Error accessing GPIO.')
RuntimeError: Error accessing GPIO.
So, after that, I created a simple python script say z.py and wrote the above code in it. Then, I activated the same Flask venv using
source venv1/bin/activate
And run the script using
python z.py
Again I got the same error. But If I run the above command as sudo
sudo python z.py
then script executed perfectly fine and I got the following response
87.0999984741211 29.399999618530273
So, now the question arrives, how do I use Adafruit_DHT package inside the Flask app with sudo permission?
I don't think setting 777 to www-data group would be the right choice. Or running the Flask app as sudo user would be a great idea.
I have tried installing Adafruit_DHT package globally with sudo, but still I have to execute z.py as sudo
So what is the correct way to do this?
I believe the package will be trying to access the device /dev/gpiomem (possibly (/dev/gpiochip0 or /dev/gpiochip1).
I think the neatest way to address this would be have those devices be owned by a group other than root and give that group permission to access the device, e.g.
sudo su
groupadd gpio
chgrp gpio /dev/gpio*
chmod g+rw /dev/gpio*
Then I'd go ahead and add your user to that group (by default this is ubuntu, but you may have created another user):
usermod -a -G gpio ubuntu
Now you've created a group called "gpio" that now has permissions to access your Pi's GPIO, and added your user to that group.
Please note, I have not tested this

Get vim on a remote server without system administration permissions

I have been given a profile (with /home directory) on a remote Linux server to work on projects that need powerful computing resources. I'd like to use Vim to edit code (mostly python) on the remote server as it can be run through a shell and doesn't require a slow GUI exchange. Currently, the Debian distribution on the remote server has a barebones vi installed and no Vim. Is there a way to install a Vim (perhaps in my home directory?) without superuser permissions?
You should be able to install vim locally, for example downloaded from a binary, or compiled from source with
git clone https://github.com/vim/vim.git
cd vim/src
make
From there, you can simply add the directory you compiled it to to PATH.

Adding read/write permission to access external HD under Catalina

I'm working on MAC application contains a command line tool.
The GUI (electron) using sudo prompt to run the command line tool written in Python.
Under Catalina, the app all needed permission to w/r for the protected directories like Desktop with no issue.
Needed permission request were added to plist and it is signed with list of entitlements.
All works well except accessing external hard drive. Any try to create folder or modifying its properties using chmod command for example return with permission denied.
Just for note, calling same command from terminal ends well with no issue
Any ideas how I can go over it?
How App can request Full disk access?

Django manage.py command having SyntaxError on ElasticBeanstalk

I'm using AWS for first time, and after uploading my Django project I wanted to know how to backup the information of the DB into a file to be able to modify the data in case I have to modify the models of my project (still on development) and keep having some population data.
I thought about the django dumpdata command, so to be able to execute it on EB through the CLI I did the following (and here is where maybe I'm doing something wrong):
- eb ssh
- sudo -s
- cd /opt/python/current/app/
- python manage.py dumpdata --natural-foreign --natural-primary -e contenttypes -e auth.Permission --indent 4 > project_dump.json
For what I understand, the first command is just to access to the SSH on Elastic Beanstalk.
The second one is to have root permissions inside the linux server to evade problems creating and opening files, etc.
The third one is just to access where the current working aplication is.
And the last is the command I have to use to dump all the data "human friendly" without restrictions to be able to use it in any other new database.
I have to say that I tried this last command on my local machine and
worked as expected without any error or warning.
So, the issue I'm facing here is that when I execute this last command, I get the following error:
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
Also I tried to skip the sudo -s to just use the permissions of the user I'm using to log on the ssh, but got: -bash: project_dump.json: Permission denied. So that is why I thought using the sudo command would help here.
In addition, I followed this known tutorial to deploy
Django+PostgreSQL on EB, so the user I'm using to access to ssh is
one in a group with AdministratorAccess permissions.
Before Trying all of this, I also looked for a way of having this information directly from AWS-RDS, but I only found a way of having a backup restored, but without being able to modify the content manually, so is not what I really need.
As on your local environment, you need to run your manage.py commands inside your correct python virtualenv and make sure the environment variables like RDS_USERNAME and RDS_PASSWORD are set. To do that, you need to:
Activate your virtualenv
Source your environment variables
As described at the end of the tutorial you mentioned, this is how to do it:
source /opt/python/run/venv/bin/activate
source /opt/python/current/env
python manage.py <your_command>
And you have to do that every time you ssh into the machine.
Note: the reason you're getting the permission denied error is that when you pipe the output of dumpdata to project_dump.json, you're trying to write in the app directory itself. Not a good idea. Try piping to > ~/project_dump.json instead (your home directory), then sudo won't be needed.

Python application permission denied

My Python program uses the terminal(system) command to perform the task on the file and script. I am going to convert this Python program into Mac OS application and Linux application using pyinstaller. I am going to pass the application installer file to my friends. However, I have following questions.
If script or file doesn't have proper permission which my program is trying to access, will Python get an error?
Running some script or opening file will require the permission of root. So is there a possible option that will prompt the user for root(admin) password or run my application with root privilege?
Thanks
Try chmod 777 filname.py, this will give the file all rights for execution and editing. There are also other modes for chmod like 755, which will also work for your case.

Categories