1

I need to copy a file from a flash disk connected to my Raspberry Pi when it boots and then execute the copied file. The reason for this is because it seems that I cannot execute the file from my USB drive.

What I did was:

sudo nano /etc/rc.local 

and added the following (in order to copy the file from the USB drive to the Pi folder - drive is the USB drive name - and then run the file):

cp /media/pi/drive/file /home/pi 
sudo ./home/pi/file & 

Both commands work when I enter it into terminal but this doesn't execute on startup.

Reason is that should I wish to change the program I can do so on another Pi, copy it onto the USB and restart the Pi to copy/replace the existing file and execute.

2

Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it.

It may be possible that the USB drive isn't mounted before executing the commands. I suggest to use a systemd unit file. Just create a new service with:

rpi ~$ sudo systemctl --full --force edit copyfile.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=Copy and execute file from USB drive
After=multi-user.target
RequiresMountsFor=/media/pi/drive

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStartPre=/bin/cp /media/pi/drive/file /home/pi
ExecStart=/home/pi/file

[Install]
WantedBy=multi-user.target

Enable the new service with:

rpi ~$ sudo systemctl enable copyfile.service

Reboot and check with:

rpi ~$ systemctl status copyfile.service
  • @ChaunBooysen Sorry, but your comments are unreadable. Please edit your question and insert the formated message into it. – Ingo May 9 at 18:52
1

/etc/rc.local runs as root so you NEVER need sudo in there.

It also runs with a limited environment and /etc as the current working directory. So you can't use relative file names. Use the full path for everything with no strange punctuation.

md /media/pi
mount /dev/sda1 /media/pi
cp /media/pi/drive/file /home/pi/script
chmod 755 /home/pi/script
su pi /home/pi/script

rc.local runs before the GUI automounter.

1

Ok I got it to work, not sure if it's the most elegant of ways but it works...

Created an extra file with the following and called the file from rc.local

#!/bin/bash

sleep 30
cp /media/pi/drive/file.sh /home/pi
sudo chmod +x /home/pi/file.sh
/home/pi/file.sh

not sure how long to wait but reckoned 30s is ok, it copies the file over, changes to executable and runs it :)

0

In addition to the advice to not use rc.local you should note that the /media is NOT automounted until the GUI starts; this is a long time after boot.

  • no really much of an answer – Chad G May 7 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.