LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-28-2023, 06:25 PM   #1
tuxthegreat
Member
 
Registered: Mar 2018
Distribution: Ubuntu Mate, OSX, Win10, ODROID-N2+
Posts: 180

Rep: Reputation: Disabled
Angry Wouldn't a systemctl service be better suited for this kind of thing vs a cron job?


I have a cronjob nonroot which is set to run at 2:01:00 and a second cronjob which is set to run at 2:05:00 every day, 7 days a week.
Code:
1 2 * * * rm -r /media/tuxthegreat/Backup/transmission
Code:
5 2 * * * cp -R /home/tuxthegreat/.config/transmission /media/tuxthegreat/Backup
My friend casually commented that there is a better way to handle such things using sysstemctl but I have no clue as where to start and how to create a systemctl service to accomplish such a feat. Any help would be greatfly appreicated guys
 
Old 10-28-2023, 07:30 PM   #2
___
Member
 
Registered: Apr 2023
Posts: 156
Blog Entries: 1

Rep: Reputation: Disabled
https://www.linuxquestions.org/quest...md-4175603950/
 
Old 10-28-2023, 07:35 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
systemd timers do have some advantages over cron and I assume your friend was suggesting using it because you can create one that is triggered after an event. In your case the second being started after a fixed time from when your first completes.

I don't know in your case if one is better then the other. You could also create a script with the two commands and then run them from a single cron job or run them from the same cron job separated by a semicolon i.e.
1 2 * * * first command; second command

They will execute in order with the first completing before the second starts.

Last edited by michaelk; 10-28-2023 at 07:37 PM.
 
1 members found this post helpful.
Old 10-28-2023, 07:41 PM   #4
tuxthegreat
Member
 
Registered: Mar 2018
Distribution: Ubuntu Mate, OSX, Win10, ODROID-N2+
Posts: 180

Original Poster
Rep: Reputation: Disabled
Wink

Quote:
Originally Posted by michaelk View Post
systemd timers do have some advantages over cron and I assume your friend was suggesting using it because you can create one that is triggered after an event. In your case the second being started after a fixed time from when your first completes.

I don't know in your case if one is better then the other. You could also create a script with the two commands and then run them from a single cron job or run them from the same cron job separated by a semicolon i.e.
1 2 * * * first command; second command

They will execute in order with the first completing before the second starts.
I thought about && the 2 commands but what takes me back a little is the timing, you see it takes a few seconds for the first command to finish it's task or rm the backup dir which is around 700MB in size, thus the 5 minute delay in the second command, I set it so that the first command would finish before the second one initiated and I don't know enough about creating cronjobs with a time delay in them, you see where I am going with this ? Step 1, rm the dir in question, Step 2 replace the dir with a new updated dir of the same kind just a lil bigger most likely. If you could come up with a cron command that does just that, I will call it a day.
 
Old 10-28-2023, 07:57 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Unless you run the command in the background i.e. add a & to the end, the first command completes before the second one starts regardless. You do not need a time delay if run together. Deleting files does not actually delete the contents just the reference to where the files are located unless you are using some sort of shred like utility. The && translates to only run the second command if the first completes successfully.
 
1 members found this post helpful.
Old 10-28-2023, 08:26 PM   #6
tuxthegreat
Member
 
Registered: Mar 2018
Distribution: Ubuntu Mate, OSX, Win10, ODROID-N2+
Posts: 180

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by michaelk View Post
Unless you run the command in the background i.e. add a & to the end, the first command completes before the second one starts regardless. You do not need a time delay if run together. Deleting files does not actually delete the contents just the reference to where the files are located unless you are using some sort of shred like utility. The && translates to only run the second command if the first completes successfully.
So how about this command
Code:
 1 2 * * * rm -r /media/tuxthegreat/Backup/transmission && cp -R /home/tuxthegreat/.config/transmission /media/tuxthegreat/Backup >/dev/null 2>&1
Should that take care of it? If so my problem is solved.
 
Old 10-28-2023, 08:31 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Yes.
 
Old 10-28-2023, 08:39 PM   #8
tuxthegreat
Member
 
Registered: Mar 2018
Distribution: Ubuntu Mate, OSX, Win10, ODROID-N2+
Posts: 180

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Yes.
Welp acccording to syslog nothing happened it didn't work.
 
Old 10-28-2023, 08:42 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
FYI: There are also many open-source "batch processing" and "workflow monitor" systems out there which are specifically designed to – for example – "launch this unit of work as soon as the prior one completes," and "do this-or-that if the process abends." Immediately after the previous step in the "job" or "workflow" completes, the next step(s) will be launched.

("abend" = an IBM term: "abnormal end." Either "it crashed," or maybe "it returned a nonzero return-code.")

"Simple cron" is very good at launching processes at specific times, but it isn't smart. There are better ways to do it.

(And, in my opinion, this is not the use-case that "systemctl" was designed to address. Don't try to "wedge" another facility "into a round hole," merely because it is there. Find the right tool for the job.)

Last edited by sundialsvcs; 10-28-2023 at 08:47 PM.
 
1 members found this post helpful.
Old 10-28-2023, 08:49 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
What time did you set it to run?
 
Old 10-28-2023, 08:56 PM   #11
tuxthegreat
Member
 
Registered: Mar 2018
Distribution: Ubuntu Mate, OSX, Win10, ODROID-N2+
Posts: 180

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
What time did you set it to run?
It's 2:55am here right now, I set it for 50 2 * * * so 2:50am and nothing showed up in syslog. Also i checked the directory and the file wasn't in there. I did service cron reload just in case.

Last edited by tuxthegreat; 10-28-2023 at 09:00 PM.
 
Old 10-28-2023, 09:04 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
I don't know at the moment. Did the job actually get saved correctly? look at the the output of
crontab -l
 
1 members found this post helpful.
Old 10-28-2023, 09:09 PM   #13
tuxthegreat
Member
 
Registered: Mar 2018
Distribution: Ubuntu Mate, OSX, Win10, ODROID-N2+
Posts: 180

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
I don't know at the moment. Did the job actually get saved correctly? look at the the output of
crontab -l
Yeah i check all my crontab edits with crontab -l after they're done. Restarting the cron did the trick @michaeik

Last edited by tuxthegreat; 10-28-2023 at 09:11 PM.
 
Old 10-28-2023, 09:14 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
You can try a test cron job command like
* * * * * env > /tmp/env.txt
 
1 members found this post helpful.
Old 10-28-2023, 10:42 PM   #15
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,151

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Not trying to be an ass but this needs to be said.

Quote:
My friend casually commented that there is a better way to handle such things using systemctl but I have no clue as where to start and how to create a systemctl service to accomplish such a feat.
Google. Seriously. It got me switched from cron > systemd timers fairly quickly. It's surprisingly easy thing to get hold of. Once you have a template just modify it as needed. I have made several services with varying times using the first timer file I made as a template. Search systemd timers
 
1 members found this post helpful.
  


Reply

Tags
crondaily



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Welcome to emergency mode! After logging in, type "journalctl -xb" to view system logs, "systemctl reboot" to reboot, "systemctl default" or grounddolphin Linux - Newbie 11 06-09-2023 05:34 PM
systemctl status postgresql-tst.service starts the service if service is stopped MarianForums Linux - Newbie 7 11-03-2018 03:02 PM
Restart systemctl*daemon​ - systemctl command not found cjosephl Linux - Newbie 7 11-11-2017 04:53 PM
Job for vncserver@:1.service failed. See 'systemctl status vncserver@:1.service' and anis123 Linux - Server 0 09-16-2015 08:44 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration