Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use sudoer to restart server in post-receive git hook

I have to restart my webserver every time I make a change to my .pm files so I am trying to set up a restart in the post-receive hook.

#!/bin/sh
GIT_WORK_TREE=/web git checkout -f
rap stop
sleep 5
rap start

When I restart manually, I have to go root and type rap stop/start. Right now I'm getting

remote: hooks/post-receive: line 3: rap: command not found 
remote: hooks/post-receive: line 5: rap: command not found

when I make a push. I think permission are blocking me and I need some help to figure it out.

like image 620
Iluvatar14 Avatar asked Dec 30 '25 16:12

Iluvatar14


1 Answers

If you need those commands used as root, you could, as in "How to execute commands as root in git post-receive hook":

  • create a separate script containing only the commands to run as root.
    #!/bin/bash
    sudo /full/path/to/rap stop
    sudo /full/path/to/rap start
  • in the post-receive script do:
    #!/bin/bash
    export GIT_WORK_TREE=/var/www/current/myapp/
    set -x
    echo "Checking out new files on production and restarting app"
    echo $USER
    git checkout -f
    sudo /home/admin/restart-myapp
  • And finally in the visudo:
    %sudo   ALL=(ALL:ALL) ALL
    admin   ALL=(ALL) NOPASSWD: /home/admin/restart-myapp
like image 190
VonC Avatar answered Jan 02 '26 14:01

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!