1

I am trying to execute a script that pushes to git every x minutes as a service, but git uses 100% CPU and high amounts of RAM while seemingly doing nothing. (I checked after 8 minutes and it was still going)

When I execute the script manually it works perfectly and takes only a few seconds.

backupToGit.sh:

#!/bin/bash
cd /home/pi/<Projectfolder>
cat /root/.ssh/id_rsa.pub
while true
do
        git add *
        git commit -m "auto backup"
        echo "------------Starting to push to Github------------"
        git push git@github.com:JustLokust/<Projectname> master
        echo "------------Finished pushing to Github------------"
        sleep 300
done

Service:

[Unit]
Description=<Service Name>

[Service]
WorkingDirectory=/home/pi/<Projectfolder>
ExecStart=/home/pi/<Projectfolder>/backupToGit.sh

[Install]
WantedBy=multi-user.target
11
  • 1
    Why do you think it's using a lot of RAM? Is it because of the memory stat in systemctl status, or something else? – Chris Down Jan 2 at 19:17
  • @ChrisDown I used the top command to check usage and saw that git was using around 300MB RAM it also randomly spikes to 300 CPU usage on a QuadCore with some other applications running. – Erik Bugdol Jan 2 at 19:48
  • Hello and welcome to Unix.se. I see you use the <something> notation: be sure it is just when posting here, and that you instead use "something" instead in the script and the command lines. < and > are special to the shell and redirect stdin and stdout (which could explain the pb: if git instead works on the whole /home/pi folder ?) – Olivier Dulac Jan 2 at 21:26
  • 1
    Do you only have 1 git running? and which one is it? : ps -ef | grep "git" | grep -v "grep" should tell you which one(s) is currently running – Olivier Dulac Jan 2 at 21:29
  • 1
    @ErikBugdol: glad you found out (that's why I suspected that ps -ef | grep git | grep -v grep may show multiple instances running in parrallel ...)/ when you take out the grep -v grep : you sometimes display the "grep git" instead, when no other git is running... You should always include | grep -v grep for those inquiries unless you need to see the grep lines themselves – Olivier Dulac Jan 2 at 22:19
1

I think I just found the Solution myself with the Help of @OlivierDulac :

The Service was only setup to start the script which would run in an endless loop, but never to stop the script. this probably resulted in the script running one more time for every time I started / restarted the Service leading to an overlap in git instances and high Resource usage.

This ultimately blocked the execution of the script.

What you need to do to Reproduce the fix: Restart your server or kill all the Remaining Processes that run the looping script.

1
  • 1
    Good practice is to use "pid" files (usually /var/run/somename.pid) and check at the beginning of the script if : the file exist, and the pid listed is indeed running the same service : in that case, do not do anything and say another is running. If it is empty, or if the pid is not present (or is not the command it is supposed to be) : you can start a new instance (and write its pid, $$, into /var/run/somename.pid) . and the stop script should: kill the pid listed in that file (IF it is the commandline it is supposed to be), and if successful delete the file. – Olivier Dulac Jan 2 at 22:26

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.