[SOLVED] Wouldn't a systemctl service be better suited for this kind of thing vs a cron job?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.