FedoraForum.org - Fedora Support Forums and Community
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2005
    Location
    British Columbia, Canada
    Posts
    871

    cron question

    Is there a way to run a script, as a regular user (with only user privileges), by placing it under the various directories such as 'cron.daily' or 'cron.monthly' or are these reserved 'only' for user 'root'?
    Robert :)

  2. #2
    Join Date
    Oct 2011
    Posts
    1,799

    Re: cron question

    Not out of the box. You could configure such service, see following for more info: https://askubuntu.com/questions/2350...n-in-user-mode. What I did is to create a very simple service and a timer placed in ~/.config/systemd/user.

    ------ cronjob.service
    Code:
    [Unit]
    Description=A service
    After=network.target
    
    [Service]
    Type=oneshot
    ExecStart=/full/path/to/script
    ----- cronjob.timer
    Code:
    [Unit]
    Description=Run cronjob daily
    
    [Timer]
    # https://unix.stackexchange.com/a/283855
    OnBootSec=15min
    OnUnitActiveSec=1d
    
    [Install]
    WantedBy=timers.target
    Check manual pages for systemd.time and systemd.timer for more info.


    EDIT:
    There is a caveat noted on the URL inside the timer file, that is that timer will run only after a full day the systemd --user is running, meaning if computer is off for example, it may take more then a day for the service to trigger again. What is cool about this, that is possible to specify fixed time the service should run, in addition to that it is possible to specify persistence, or in another words the service will run if the last schedule was missed as soon as the machine is able to. That is sort of anacron replacement, but with a possibility to specify a fixed time.

    Code:
    OnCalendar=9:10
    Persistent=true
    Last edited by srakitnican; 8th January 2018 at 10:49 PM.

  3. #3
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,166

    Re: cron question

    You can create a user crontab by going to terminal mode as a user and typing crontab -e
    It will be the user's crontab. Log in as root and normally you would create a crontab for root

    A crontab has columns as shown below.

    You can run jobs daily, on reboot, or according to some other rules.


    Code:
    #########################################################
    # string         meaning
    # ------         -------
    # @reboot        Run once, at startup.
    # @yearly        Run once a year, "0 0 1 1 *".
    # @annually      (same as @yearly)
    # @monthly       Run once a month, "0 0 1 * *".
    # @weekly        Run once a week, "0 0 * * 0".
    # @daily         Run once a day, "0 0 * * *".
    # @midnight      (same as @daily)
    # @hourly        Run once an hour, "0 * * * *".
    #
    #minute hr     mnthday  Mth     DoW     COMMAND 
    PATH=$PATH:/sbin:/bin:/usr/local/bin
    #........................................Minute of the hour
    #|      .................................Hour in the day (0..23)
    #|      |       .........................Day of month, 1..31 (mon,tue,wed)
    #|      |       |       .................Month (1.12) Jan, Feb.. Dec
    #|      |       |       |       .........day of the week 0-6  7==0
    #|      |       |       |       |       |command to be executed
    #V      V       V       V       V       V
    @daily                                  find   /home -name core 2>/dev/null | xargs rm -f >/dev/null 2>&1
    #
    #to run something on the last day of the month at 5:30am     
    30      5     30-31     *       *       [ `date -d tomorrow +%d` -eq '01' ] && do_month_end_thing                                       
    30      5     28-29     feb     *       [ `date -d tomorrow +%d` -eq '01' ] && do_month_end_thing                                                                                                                                               
    #                                                                                                
    
    You can setup the jobs to run daily, weekly, monthly, yearly, from one crontab.

    You can organize your crontab into sections of daily, weekly, monthly, trimesterly etc.
    For more info info/man crontab
    Last edited by lsatenstein; 10th January 2018 at 04:28 PM. Reason: clarificatiion
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  4. #4
    Join Date
    Nov 2008
    Posts
    415

    Re: cron question

    You can also have a user cron run a user anacron - mimicking the way the system cron and anacron work, for the same reason: in case your system isn't running 24/7.

  5. #5
    Join Date
    Jan 2015
    Location
    Al Ain, UAE
    Posts
    797

    Re: cron question

    There are the commands su and sg.

    Those two can be used to run a program as another user or group. Since cron runs as root, it can run a program as a user without having to ask for a password if you use these utilities to launch it.
    --
    Have fun!
    http://www.aeronetworks.ca

  6. #6
    Join Date
    Oct 2005
    Location
    British Columbia, Canada
    Posts
    871

    Re: cron question

    I always wanted to know how to do this. Thanks so much for the code example, I will be trying it.
    Robert :)

  7. #7
    Join Date
    Oct 2005
    Location
    British Columbia, Canada
    Posts
    871

    Re: cron question

    I have always done things the way you show in your response. I may very well use this approach but I was just wondering how to use the 'cron.hourly' etc in a novel way. Thanks for the details and the URL. Can you tell me how to get notified by Email when there are responses to my threads/posts. On the new system I cannot seem to see it, thanks.
    Robert :)

  8. #8
    Join Date
    Oct 2005
    Location
    British Columbia, Canada
    Posts
    871

    Re: cron question

    I have never used 'anacron' although I have seen it mentioned before. I will look into this approach, thanks.
    Robert :)

  9. #9
    Join Date
    Oct 2005
    Location
    British Columbia, Canada
    Posts
    871

    Re: cron question

    I would never have thought of this. I will experiment with this approach. Thank you very much.
    Robert :)

  10. #10
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,166

    Re: cron question

    Robert, do info cron and crontab. For example, I use the reboot entry, of cfon of which you can have more than one. I also use a crontab's option to start some time after a reboot. I do my backup every night, week, and end of month. My nightly backup is wiped clean after my weekly one, and my "last day of the month " backup cleans all but the last weekly.
    I have to say, cron/crontab is a great utility.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

Similar Threads

  1. cron job question
    By rubikssolver in forum Using Fedora
    Replies: 2
    Last Post: 14th September 2009, 02:48 AM
  2. Cron Question
    By comptech520 in forum Using Fedora
    Replies: 4
    Last Post: 28th August 2007, 08:00 PM
  3. cron question
    By Flatron in forum Using Fedora
    Replies: 2
    Last Post: 29th December 2005, 06:36 PM
  4. cron Question
    By h4d in forum Using Fedora
    Replies: 7
    Last Post: 7th December 2004, 05:27 PM
  5. A little cron-question...
    By badfinger in forum Linux Chat
    Replies: 2
    Last Post: 13th October 2004, 10:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •