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

Author Topic: How to make a simple Cron script for Linux?  (Read 5614 times)

0 Members and 1 Guest are viewing this topic.

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
How to make a simple Cron script for Linux?
« on: May 07, 2018, 10:56:29 PM »
Can anybody here tell me how to write a simple Cron script?
Basic job:
 It will run daily.
 It will copy  a jpg  image file

Now when  I get that far, I will rotate the file naming from a list
The list is  this:
sig1 sig2 sig3 sig4 sig5 sig6 sig7

It will start by making  sig.jpg from the next item in the list.

So on Monday, visitors will see the image from the sig1 file.On Tuesday, the sig2 file. And so on.

I guess I could just do this  with FrontPage extensions, but I don't think the server has that. I have not seen it for a long time. Maybe it has been forgotten.

It does not have to be Cron.  Any suitable script will do.
Any suggestions?  :)

nil

  • Global Moderator


  • Intermediate
  • Thanked: 15
    • Experience: Experienced
    • OS: Linux variant
    Re: How to make a simple Cron script for Linux?
    « Reply #1 on: May 08, 2018, 05:55:18 PM »
    i think this will work

    Code: [Select]
    #!/bin/bash
    symlink="$HOME/work/temp/geek9pm-cron/sig.jpg"
    prefix="sig"
    ext="jpg"
    num="$(date '+%u')"
    filepath="$HOME/work/temp/geek9pm-cron/files"
    filename="$prefix$num.$ext"
    todaysimage="$filepath/$filename"
    if [ -e "$todaysimage" ]
    then
      echo "Source: $todaysimage"
      echo "Dest:   $symlink"
      if ln -sf "$todaysimage" "$symlink";
      then
        echo "Done"
        exit 0
      else
        echo "Link failed"
        exit 1
      fi
    else
      echo "Image $todaysimage doesn't exist, exiting"
      exit 1
    fi

    then

    Code: [Select]
    crontab -e
    add line

    Code: [Select]
    1 0 * * * /path/to/yourscript
    by default Monday is day 1, Sunday is day 7, so this would symlink files/sig1.jpg to sig.jpg at 12:01 Monday morning

    if your web server serves symlinks this should work
    otherwise just use cp instead of ln -sf
    « Last Edit: May 08, 2018, 06:34:57 PM by nil »
    Do not communicate by sharing memory; instead, share memory by communicating.

    --Effective Go

    Geek-9pm

      Topic Starter

      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: How to make a simple Cron script for Linux?
    « Reply #2 on: May 08, 2018, 08:05:57 PM »
    Many thanks, I will give it a try.   :D