I have a cronjob that runs every two minutes and sets a servo. (Example from: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-8-using-a-servo-motor/software)

My bash-script calculates the needed millisecond setting and ends with:

gpio -g mode 18 pwm
gpio pwm-ms
gpio pwmc 192
gpio pwmr 2000
gpio -g pwm 18 "$SERVO_MS"
  • Is setting the mode everytime I run the script hazardous?
  • Do I otherwise need to cleanup gpio after my script runs?

1 Answer 1


If you don't set the mode, it will not work. No, it is not hazardous.

No, you do not need to clean up GPIO after the script runs. Indeed, if you do so, the servo will not work properly.

  • But I could set the mode and the pwmc, pwmr settings once on startup of the raspberrypi and then just issue the last command, every 120 seconds. Would that be better? Feb 3, 2017 at 8:29
  • 1
    @FabianZeindl Yes you could. As long as no other software touches GPIO18 or alters the PWM settings you could argue that is better. I think that would come down to a matter of taste. I am not sure which I would choose to do, but I would probably leave it as it is in this case.
    – joan
    Feb 3, 2017 at 9:30
  • Then I'll leave it as it is, for simplicity's sake. Feb 4, 2017 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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