# GPIO Change Pin State script



## Phishfry (Jun 27, 2018)

Well tonight I was making a script to change GPIO pins on bootup.
/etc/rc.d/gpiocfg

```
#!/bin/sh
gpioctl -c 0 OUT PP
gpioctl -c 1 OUT PP
gpioctl -c 2 OUT PP
gpioctl -c 3 OUT PP
gpioctl -lv
```
I was writing the outline for an rc.d startup script, so I wrote the file premise in the /etc/rc.d directory.
I had also chmod 755 & chmod +x 'ed the file in preparation for rc.d scripting.

When I rebooted for another reason, the script ran. I was not expecting it without the rest of the dummy rc.d entries.
Not too proud to say I learned something by accident tonight.
I really didn't want a full blown service or PID and the small script worked great. Changed the pin state and showed status.
The pin states might change later these are just safer boot settings with stuff connected.

Have I done it the wrong way? Do I really need to go back and use all this stuff:
https://www.freebsd.org/doc/en_US.ISO8859-1/articles/rc-scripting/rcng-dummy.html


----------



## pboehmer (Jun 27, 2018)

Yeah, anything thats executable in rc.d will be ran.  We have some startup scripts dating back to the 3.4 release that are still in place today on our 11.2 boxes.  First of all, regardless if you use rcng or a basic shell script, I would highly recommend for sanity that you put the script in /usr/local/etc/rc.d.  Second of all, if you go with basic shell script, you and anyone else who manages the box will lose some functionality (off the top of my head):

- Ability to use `service` (may trip up newer administrators)
- Script will be executed in the alphabetical order of the name of the script.  So, if you need script z_script.sh to execute before a_scripts.sh, you'll have to rename it to 1z_script.sh.

And a couple of other things that I most likely missed.  General advice (not practicing what I preach, old habits die hard) would be to get in the habit of writing rcng scripts.


----------



## Phishfry (Jul 1, 2018)

Ideally I would set these GPIO in coreboot on the APU2/3 but that's another topic.
On Arm the GPIO's would be manipulated at boot by the device tree.


----------



## Nicola Mingotti (Aug 27, 2018)

If a full service is not needed I usually put the code in /etc/rc.local or, more recently is tend to use a "@reboot" lines in `cron`. 

Is there any bad in these ways ?


----------

