2
votes

I have installed a backup program called rclone on my raspberry pi which is running Debian, I have successfully ran the cmd in the shell to backup a folder to google drive but I really need to be able to do so each time a take a photo with my python script, I have little experience in Linux compared to others and I thought that if I made a shell script with a basic shebang of

 #!/bin/sh 

or

 #!/bin/bash 

then the cmd below

rclone copy /var/www/html/camera_images pictures::folder1

I then made the .sh file executable, and this works if I just click it in the folder and execute but if I try to call that .sh script from python with

os.system('sh /home/pi/py/upload.sh')

or

os.system(' rclone copy /var/www/html/camera_images pictures::folder1 ')

I get an error in the shell saying

Failed to load config file "/root/.rclone.conf" using default - no such directory.

But the .conf is located in /home/pi as it should be. and if i try

os.system(' sh rclone copy /var/www/html/camera_images pictures::folder1 ')

I get

sh: 0: Cant open rclone.

How can I can run the copy cmd or a script to do so from python?

this is how i installed rclone

  1. cd

  2. wget http://downloads.rclone.org/rclone-v1.34-linux-arm.zip

  3. unzip rclone-v1.34-linux-arm.zip

  4. cd rclone-v1.34-linux-arm

  5. sudo cp rclone /usr/sbin/

  6. sudo chown root:root /usr/sbin/rclone

  7. sudo chmod 755 /usr/sbin/rclone

  8. sudo mkdir -p /usr/local/share/man/man1

  9. sudo cp rclone.1 /usr/local/share/man/man1/

  10. sudo mandb

  11. rclone config

3
Make sure your rclone program is in your system path - Jean-François Fabre
the rclone folder containing the rclone.exe is in my root directory /home/pi as it should be, and i can just open the shell and type the cmd rclone copy /var/www/html/camera_images pictures::folder1 and it works even if i change directory. - Edwin Martin
Ok remove the sh prefix and tell us - Jean-François Fabre
k 1 min i just loading it now - Edwin Martin
I tried both os.system('/home/pi/py/upload.sh') and os.system(' rclone copy /var/www/html/camera_images pictures::folder1 ') i both give the same message, Failed to load config file "/root/.config.conf" - using defaults: open /root/.rclone.conf: no such directory. - Edwin Martin

3 Answers

2
votes

Use --config in your rclone command

From docs: --config string Config file. (default /home/ncw/.rclone.conf")

Your command should looks like: os.system(' sh rclone copy --config /home/pi/.rclone.conf /var/www/html/camera_images pictures::folder1 ')

1
votes

You should be using subprocess module instead of os.system.

You can use subprocess.Popen to create a process and give it a working directory.

subprocess.Popen(your_command, cwd=path_to_your_executable_dir, shell=True)

(Use shell=True to pass a simple string command among other conveniences).

The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.

On Unix with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. That is to say, Popen does the equivalent of: ....

0
votes

Thank you every one :)

I have it working now with

os.system(' rclone copy --config /home/pi/.rclone.conf /var/www/html/camera_images pictures::folder1 ')

Note that if i put sh at the start i got the error sh: 0: Can't open rclone though i read yesterday about putting something like ,:0 at the end as a return value ? either way it works without the sh.

and the subprocess works too which i shall use instead.

subprocess.Popen('rclone copy --config /home/pi/.rclone.conf /var/www/html/camera_images pictures::folder1', shell=True)