Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Limit CPU Usage of a Cron Job?  (Read 10188 times)

0 Members and 1 Guest are viewing this topic.

Zylstra

    Topic Starter
  • Moderator


  • Hacker

  • The Techinator!
  • Thanked: 45
    • Yes
    • Technology News and Information
  • Certifications: List
  • Computer: Specs
  • Experience: Guru
  • OS: Windows 7
Limit CPU Usage of a Cron Job?
« on: December 18, 2009, 11:28:54 PM »
ʖ Is it possible to limit the CPU Usage of a cron job?

On my server, I have a schedule cron job that runs nightly at midnight through Webmin which backs up my home folder to a second drive. Unfortunately, this seems to rack up the load average higher than what I would like, and it causes the Apache response time to become very delayed. (IRC does fine for some reason).

Just by editing the cron command, is it possible to prevent a task from using up so many resources? I recently changed it to just update new files instead of all files, but I would still like to reduce the CPU usage. (Google just got my site indexed. I'm so happy!)

GuanHsu



    Newbie

    Re: Limit CPU Usage of a Cron Job?
    « Reply #1 on: December 20, 2009, 09:56:29 AM »
    Zystra,

    You did not mention what OS you run on the server.  Based on the question, I gather that it is a Linux/UNIX type system. 

    You can use nice, or the variant of similar command to set scheduling priorities for your shell, in your cron to lower the priority of the backup process.  This should work better than limiting the cpu usage.  OS generally can do a good job arranging how much CPU to allocate.  Setting priority work better to take advantage of the expertise of kernel developers in optimizing the OS than trying to dictate a hard number of CPU percentage.   

    After that is done, you should also evaluate the backup process.  Back up is typically I/O intense and demanding on system resources.  You might consider using more efficient method of backup.  It can vary a great deal depending on what kind of task your server performs.    One popular method for websites or blog sites is use rsync to harddisk.   rsync can backs up only changes.  It is efficient enough that people even use it to sync hourly, definitely more often than once a day.   If you can afford to rsync to a 2nd server, call it your failover server, than you can backup the failover server in a more traditional manner that you are doing now.   These are just a few thoughts.    Of course, your mileage might vary.

    Good luck!

    Guan

    PS: you have a nice post on why Linux is better than Windows for average users.  That is refreshing coming from an expert Windows user, according to your profile.

    Zylstra

      Topic Starter
    • Moderator


    • Hacker

    • The Techinator!
    • Thanked: 45
      • Yes
      • Technology News and Information
    • Certifications: List
    • Computer: Specs
    • Experience: Guru
    • OS: Windows 7
    Re: Limit CPU Usage of a Cron Job?
    « Reply #2 on: December 20, 2009, 10:16:08 PM »
    Guan,

    Thank you for your help, I will be looking into nice sometime soon.
    It occurred to me, however, that I may not be able to edit the cron job task and have Webmin still recognize it.

    I am in Ubuntu, by the way.

    In the past, I investigated rsync, but the only other computer that I have which is capable of storing larger backups runs Windows and I could not find a good Windows rsync option. Currently, the backup that is giving me problems copies the home folder to a secondary drive. The second backup, the backup of the backup to another computer via. FTP, strangely does not cause problems -- not even network response time.

    Thanks for the comment on my article, I use both Windows and Linux -- both really have their advantages and disadvantages. Once I found out that it was in fact possible to game on Linux via. PlayOnLinux (well, not all games,) I was convinced that switching would be beneficial. It has provided a great experience in alternative operating systems as well.


    GuanHsu



      Newbie

      Re: Limit CPU Usage of a Cron Job?
      « Reply #3 on: December 21, 2009, 06:51:03 PM »
      I don't know Webmin but I would imagine it should be able to handle nice.   Just add nice in front of your backup command.  For example, if you use tar for backup (Oops!  I dated myself right there), your crontab should have an entry like

      0 15 0 * * *     operator  nice +15 sudo tar ....

      assuming you have a user called operator to run backup via sudo without password.   

      If you find a good Windows rsync implementation, I would like to use it too.   The rest of my household still use Windows.   It is hard to change peope who doesn't see the need and doens't want to be changed  ;)