Tag: cronjob

  • Cron Job fails with Error Message “(getpwnam() failed): No such file or directory”

    Cron Job fails with Error Message “(getpwnam() failed): No such file or directory”

    On a CentOS server, cronjob did not work. Checking the log file found the following error message in /var/log/cron

    Oct 24 18:35:01 CentOS-75-64-minimal crond[1546]: (/usr/local/bin/monitor-mysql) ERROR (getpwnam() failed)
    

    The error was because no user was specified in the cronjob. When you use cronjob in file /etc/crontab, you need to specify the username before the command in cronhjob.

    On the server, I had the following cronjob

    */5 * * * * /usr/local/bin/monitor-mysql > /var/log/mysql-monitor.log
    

    To fix the error, I added the user name before the command as below.

    */5 * * * * root /usr/local/bin/monitor-mysql > /var/log/mysql-monitor.log
    

    See cronjob

  • Run rsync if not running using cronjob

    Run rsync if not running using cronjob

    I want to rsync files from one server to another server every 5 minutes, but only want to start the rsync if the previous rsync command has finished. Time for one rsync depends on how much data changed on the source server, so the time taken to finish rsync cronjob varies.

    Create file rsync.sh

    #!/bin/bash
    
    if ! pgrep -x "rsync" > /dev/null
    then
        rsync -avzP /var/www/html/files/  [email protected]:/var/www/html/files/
    fi
    

    Set cronjob like

    */10 * * * * /root/rsync.sh > /dev/null
    
  • How to Enable Magento 2 Cronjob

    Magento uses cron jobs for numerous features to schedule activities. To enable the cron job in Magento 2, log in to the server using SSH. Switch to the appropriate user if you are logged in as root. Now run command

    php bin/magento cron:install
    

    If you need to remove the cronjob, run

    php bin/magento cron:remove
    

    set cronjob in magento 2

    See Magento

  • Set vim as default cronjob editor

    Method 1

    vi /etc/bashrc
    

    Find

    export EDITOR="pico"
    export VISUAL="pico"
    

    Replace with

    export EDITOR="vi"
    export VISUAL="vi"
    

    Method 2

    edit .bash_profile

    vi /root/.bash_profile
    

    Add

    export VISUAL=vi
    

    Once added, .bash_profile will look like

    root@server54 [~]# cat .bash_profile
    # .bash_profile
    
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
            . ~/.bashrc
    fi
    
    # User specific environment and startup programs
    
    PATH=$PATH:$HOME/bin
    
    export VISUAL=vi
    export PATH
    unset USERNAME
    uptime
    root@server54 [~]#
    

    Method 3

    If you need it done one time only, run following commands on command prompt.

    VISUAL=vi; export VISUAL
    
  • Start a program after reboot using cronjob

    To start a program using cronjob, create a cronjob like

    @reboot /root/start.sh
    

    On system boot, the command “/root/start.sh” will be executed.

  • cronjob

    crontab
    Start a program after reboot using cronjob
    Running wp-cron with cronjob
    Cronjob for Magento 1.9
    set vi as default cronjob editor
    Cron Job fails with Error Message “(getpwnam() failed): No such file or directory”

    Cronjob Timing

    * * * * *  = every minute
    */5 * * * *  = every 5 minute
    0 * * * *  = every hour
    0 * * * *  = every day at 00:00 hour.
    0 0 * * 0  = every Sunday at 0 minutes, 0 hour.
    

    Here is how it works.

    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed
    

    Disable cronjob output

    To disable cronjob output, add the following to the end of the script

     > /dev/null 2>&1
    

    or

     1> /dev/null 2> /dev/null
    

    Save cronjob result to file

    To save the result of a cronjob to a file, use

    20 5 * * * /bin/bash /usr/serverok/mysqldb-backup.sh > /var/log/mysqldb-backup.log 2>&1
    

    List all cronjobs

    To list all cronjob, run

    crontab -l
    

    To edit all cronjobs, run

    crontab -e