You are Here:
Linux Lite 4.8 Final is now available for download and installation



Cronjob for a script

Author (Read 1581 times)

0 Members and 2 Guests are viewing this topic.

Cronjob for a script
« on: September 18, 2018, 04:39:25 PM »
 

Moltke

  • Gold Level Poster
  • *******
  • 615
    Posts
  • Country: ve
  • Reputation: 77
  • Linux Lite Member

  • Linux Lite: 3.8 64bit

  • CPU: amd athlon 64 x2

  • MEMORY: 4Gb

  • VIDEO CARD: amd radeon hd 6750

  • Kernel: 4.x
Hi everyone! Hope you're all having a nice life! :)

I wrote a very simple script to copy files from one dir into another one, I want to create a cronjob so it runs once a week - on Sundays - but I'm not sure whether if it's correct or not. Can you take a look and tell me so?
this is the cronjob:

0 1  * 1-12 SUN /home/myusername/dir/cp.sh 
or

0 1  * 1-12 SUN /bin/bash /home/myusername/dir/cp.sh

I've read quite a few posts all over the web but I'm still confused. Thanks in advance for your answers.

Last Edit: September 23, 2018, 07:46:18 PM by Moltke
Without each others help there ain't no hope for us :)
 


Re: Cronjob for a script
« Reply #1 on: September 18, 2018, 06:20:45 PM »
 

bitsnpcs

  • Platinum Level Poster
  • **********
  • 3185
    Posts
  • Country: 00
  • Reputation: 300

  • Linux Lite: 3.2 64bit
Hello Moltke,

I haven't done this before, so it is just results from searches, I hope they help a bit though in finding the solution -

https://crontab.guru/

I used to make the  below -

Code: [Select]
0 1 * 1-12 0

It will do the task at 01:00 hours on Sunday in every months from January to December.
It says for Sundays if you wish you can use instead of , 0, sun.
I am unsure if whether if a users week setting begins on Sunday or Monday may alter the numerical codes or if the crontab.guru bases this on using a geolocation script of the visitor, so I can see where using the day name in the week may be of use if it turns out to be presenting a difficulty.


Now the time is done it needs the command -
Code: [Select]
cp /origin /destination
example

Code: [Select]
0 1 * 1-12 0 cp /path/to/directory /path/to/destination
I also found this link about using rsync for this type of task, I have not used or learnt about rsync yet so its just search result info. (I changed it to add the cron info from the beginning of the post)

https://serverfault.com/questions/259938/cron-job-to-copy-file-from-one-location-to-another-for-new-files-daily/259949


Code: [Select]
0 1 * 1-12 0 /usr/bin/rsync -a /origin /destination
Last Edit: September 18, 2018, 06:28:55 PM by bitsnpcs
 

Re: Cronjob for a script
« Reply #2 on: September 18, 2018, 07:43:31 PM »
 

Moltke

  • Gold Level Poster
  • *******
  • 615
    Posts
  • Country: ve
  • Reputation: 77
  • Linux Lite Member

  • Linux Lite: 3.8 64bit

  • CPU: amd athlon 64 x2

  • MEMORY: 4Gb

  • VIDEO CARD: amd radeon hd 6750

  • Kernel: 4.x
thank you @bitsnpcs :)

I also used that site https://crontab.guru/ when creating this cronjob. Your info is ok but in my case the script is in the source directory, so according to what I read and what they told me in another forum the cronjob should look like:

0 1 * *  SUN /home/moltke/dir/cp.sh  #the * *  are wildcards so meaning the job will run every week and every month.

Since the script contains the command which is
Code: [Select]
$ cp -r -n /path/to/cp into there's no need to add the command. Regarding rsync I think cp is good enough to do te job. BTW, you're right; instead of SUN one could use 0 or 7. There are another ways to do this it seems according to what I read, like using @weekly instead of the whole "0 1 * * SUN/0/7" but I need to read more about it before using it. For now this is supposed to work. :) I guess I'll find out on Monday  ::)
Last Edit: September 18, 2018, 07:48:51 PM by Moltke
Without each others help there ain't no hope for us :)
 

Re: Cronjob for a script
« Reply #3 on: September 18, 2018, 07:48:49 PM »
 

bitsnpcs

  • Platinum Level Poster
  • **********
  • 3185
    Posts
  • Country: 00
  • Reputation: 300

  • Linux Lite: 3.2 64bit
Glad you have solved it :)
 

Re: Cronjob for a script
« Reply #4 on: September 18, 2018, 07:57:02 PM »
 

Moltke

  • Gold Level Poster
  • *******
  • 615
    Posts
  • Country: ve
  • Reputation: 77
  • Linux Lite Member

  • Linux Lite: 3.8 64bit

  • CPU: amd athlon 64 x2

  • MEMORY: 4Gb

  • VIDEO CARD: amd radeon hd 6750

  • Kernel: 4.x
Quote
Glad you have solved it :)
thank you :)

BTW, it seems that the cron tool's smart enough so it'll tell you about any syntax error. So I wouldn't have been able to save it unless properly typed/written.
Without each others help there ain't no hope for us :)
 

Re: Cronjob for a script
« Reply #5 on: September 18, 2018, 08:46:59 PM »
 

bitsnpcs

  • Platinum Level Poster
  • **********
  • 3185
    Posts
  • Country: 00
  • Reputation: 300

  • Linux Lite: 3.2 64bit
Quote
Glad you have solved it :)
thank you :)

BTW, it seems that the cron tool's smart enough so it'll tell you about any syntax error. So I wouldn't have been able to save it unless properly typed/written.

That's very useful functionality :)
 


Tags:
 


Linux Lite 4.8 Final is now available for download and installation