1

I am setting up a Raspberry Pi Zero with a camera and want the Pi to automatically log on to the terminal then run a motion detecting script in a infinite loop. I put it in the /etc/profile and the script has two threads I think.

The problem is I forgot to setup the Pi to automatically logon and now when I type in the logon info it automatically runs my script making it impossible to stop and set up the Pi to automatically logon. I have tried Ctrl + Z, D and X but no luck.

  • 1
    I guess you added the script in ~/.profile? Ok... first, your problem: how to kill the script: I hope this works: ctrl+c should be able to kill it... unless you are using more than one thread and then it's a wild guess. Then, the other problem: If you want something to start automatically, you should either run it from /etc/rc.local or with a custom systemd service (also cron can do it but I have never tried it so won't bet on it yet... plus with systemd you can stop/restart, etc). – eftshift0 Apr 28 '18 at 2:56
  • Yeah I put it in the /etc/profile and the script has two threads I think – kwjamesblond Apr 28 '18 at 3:36
  • That can't be good, can it? Do you have, by any chance, physical access to the computer? If that's the case, shut it down, put the sd card on a computer (I mean, like a laptop or a desktop or a server, you get the idea), mount the / partition of the sd card (/dev/mmcblk0p2, probably) and edit the profile file so that it doesn't work when you start the terminal (or add a & at the end so that it goes into the background). Another possibility: Did you happen to set up a different user on that board? If that's the case, try to log in with that other user, then sudo kill the process. – eftshift0 Apr 28 '18 at 3:42
  • I plugged the sd card in a computer and fixed the issue, so if I need to have a script run automatically at startup what is the best way to do this so I can stop it if I need to change something and access the terminal. – kwjamesblond Apr 29 '18 at 4:42
  • @eftshift0 I suggest you make your comments an answer because it is. – Ingo Apr 29 '18 at 17:00
1

If you have another computer with Linux, You can take the SD card from Raspberry PI and open on other computer. On second partition you have main filestsystem of Raspberry PI. Find your .profile file and edit. After that, return SD card to Raspberry PI.

About running some scripts at startup: Do not put anythin that can hangs or take long time into .profile or .bashrc files. It's risky. You should put ther only scripts that you need after login not after bootup. What you put into .profie or .bashrc file is executed every time you login. If you want to start something after bootup you have 2 choices: Simplest - use crontab with @reboot instruction. Bulletproof - create service.

0

You can try spamming ALT + F4, at least this worked for me in Kali, where I had no GUI, only terminal.

To avoid such softlocks in the future, make sure you run your scripts in the background with the "&" command at the end of the script execution command. Even better, the infinite loop part of your script could be a separate function which is called in the background inside your script. Also note that, the termination of the background scripts should be handled as well.

Now, in order to run scripts at boot, you could try adding it to the .bashrc file located in your home folder. For example: ./myscript.sh &

If you want the script to run once and only once, independently from the user, you need to add it to /etc/rc.local. That is because the .bashrc file is user specific and runs again when a new bash shell is spawned for this user. There is an official guide from raspberry pi site here : https://www.raspberrypi.org/documentation/linux/usage/rc-local.md

  • To put scripts into .bashrc to run at boot time does not make sense. .bashrc is only started if you start a no login shell and not at boot time. – Ingo Apr 30 '18 at 8:07
  • I agree, I'm sorry if I wasn't clear, but this is the case of Kali Linux where you have no GUI,I you login and a bash terminal is spawned. The OP didn't state the OS, so I presented a case. – Leajian Apr 30 '18 at 8:55
0

Do you have, by any chance, physical access to the computer? If that's the case, shut it down, put the sd card on a computer (I mean, like a laptop or a desktop or a server, you get the idea), mount the / partition of the sd card (/dev/mmcblk0p2, probably) and edit the profile file so that it doesn't work when you start the terminal (or add a & at the end so that it goes into the background). Another possibility: Did you happen to set up a different user on that board? If that's the case, try to log in with that other user, then sudo kill the process (hoping the user is a sudoer).

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.