kaksi
Posts: 92
Joined: Tue Mar 10, 2015 6:19 am

Writing files with owner=pi instead of owner=root

Tue Nov 28, 2017 12:38 pm

I have a Python program with the instruction:

Code: Select all

camera.capture(jpg_name)

I call Python from /etc/profile:

Code: Select all

sudo python /home/pi/cam/PI_CAM.PY

I guess the owner is root because I call the script with sudo. The obvious solution does not work: if I call the script w/o sudo, the script does not run.

One way is chown after each snap. Not very elegant.

BTW, this script is re-executed when I connect with ssh or putty - not my top list problem now, but it would be nice to solve it too.

Any idea?

User avatar
RaTTuS
Posts: 10415
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Writing files with owner=pi instead of owner=root

Tue Nov 28, 2017 12:43 pm

change it so it does not run as root
change whatever you need that needs it to be root so it is not
dont stuff it in /etc/profile
I assume you want it to auto run
then put something like

Code: Select all

if [ $(tty) == /dev/tty1 ]; then
   ./script
fi

.bashrc in user pi home directory
post your script here so we can dissemble it to make it so it does not need root.
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
topguy
Posts: 5685
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Writing files with owner=pi instead of owner=root

Tue Nov 28, 2017 4:12 pm

Also check the owner of "cam" directory, the script itself and the directory where he pictures are saved.

User avatar
DougieLawson
Posts: 35810
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Writing files with owner=pi instead of owner=root

Tue Nov 28, 2017 5:44 pm

Stop running everything as sudo. 99.99999% of programs DO NOT need to run as root.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

kaksi
Posts: 92
Joined: Tue Mar 10, 2015 6:19 am

Re: Writing files with owner=pi instead of owner=root

Tue Nov 28, 2017 7:41 pm

RaTTuS wrote:
Tue Nov 28, 2017 12:43 pm

put something like

Code: Select all

if [ $(tty) == /dev/tty1 ]; then
   ./script
fi

.bashrc in user pi home directory
Works perfectly. Thank you very much.

If it comes handy to others, what to avoid:
- do not save these lines with notepad and include the file with nano; .bashrc will be saved in DOS format (some scripts do not mind, this does)
- when typing the code, spaces matter a lot

W. H. Heydt
Posts: 10768
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Writing files with owner=pi instead of owner=root

Tue Nov 28, 2017 8:29 pm

kaksi wrote:
Tue Nov 28, 2017 7:41 pm
- do not save these lines with notepad and include the file with nano; .bashrc will be saved in DOS format (some scripts do not mind, this does)
That sort of problem is easily fixed...at least in vi. If you have to strip off the extraneous ctl-M, then use :g/^v^m/s/// where ^v is ctl-V and *m is ctl-M. To get rid of blank lines you can use :g/^$/d where * is really a caret and $ is really a dollar sign.

Ctl-V acts as an escape character in vi and prevents the ctl-M from being an actual carriage return. ^ is the begging of the line and $ is the end of the line, so ^$ is a line with nothing on it.

DirkS
Posts: 9902
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Writing files with owner=pi instead of owner=root

Tue Nov 28, 2017 8:54 pm

W. H. Heydt wrote:
Tue Nov 28, 2017 8:29 pm
That sort of problem is easily fixed...at least in vi
And for simple people like me: use dos2unix

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Writing files with owner=pi instead of owner=root

Wed Nov 29, 2017 2:22 am

DirkS wrote:
Tue Nov 28, 2017 8:54 pm
W. H. Heydt wrote:
Tue Nov 28, 2017 8:29 pm
That sort of problem is easily fixed...at least in vi
And for simple people like me: use dos2unix
If you have to edit on Windows (don't know why, vi's fine), use notepad++

Return to “Beginners”