LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Can You Load Only Part of a Crontab File? (https://www.linuxquestions.org/questions/linux-server-73/can-you-load-only-part-of-a-crontab-file-4175609889/)

RPN 07-14-2017 09:52 PM

Can You Load Only Part of a Crontab File?
 
What I would like to know is it possible to modify a lengthy crontab file so that cron only loads the first “X” number of lines? I am looking for something that would be similar to adding an “exit” line in the middle of a bash script

I know I can comment out the active lines with “#” and use “##” to mark those lines that now have a single “#” because the lines are not currently being used and are there for historical purposes, but it would nice if there was an easier way of preventing a whole block of lines below a certain line from being active.

frankbell 07-14-2017 10:01 PM

I've never tried it, but I believe the short answer is "No."

RPN 07-14-2017 10:25 PM

Quote:

Originally Posted by frankbell (Post 5735093)
I've never tried it, but I believe the short answer is "No."

That is what I figured but I also figured it would not hurt to ask. It would have made complying with the first line of your sig much easier. :)

syg00 07-14-2017 10:35 PM

sed ?

frankbell 07-14-2017 11:14 PM

Quote:

sed ?
Tell us more. If it's a matter of using sed to edit the file, I don't think that's what OP is asking, but I'm curious.

RPN 07-14-2017 11:25 PM

Quote:

Originally Posted by syg00 (Post 5735099)
sed ?

I was not clever enough to do it on the command line. but I was able to put together a two line bash script where I could pass the number of lines I wanted as an argument. The first line generated a temporary file and the second line loaded it into cron. That is what I was trying to do. Thanks.


Edit: I just discovered it not only loaded the temporary file into memory but it created a new shortened crontab file as well. That is not what I wanted. Luckily I have a backup and I will rework the script to use a renamed crontab file so it will not get overwritten.

frankbell 07-14-2017 11:30 PM

Quote:

I was not clever enough to do it on the command line. but I was able to put together a two line bash script where I could pass the number of lines I wanted as an argument. The first line generated a temporary file and the second line loaded it into cron. That is what I was trying to do. Thanks.
I am impressed. That sounds like a very creative solution.

Would you be willing to post the script, being sure to surround it with "code" tags, which become available when you click the "Go Advanced" button at the bottom of the "compose post" window?

RPN 07-15-2017 12:25 AM

I normally don't but I will put on my fireproof suit this time :)

Code:

sed -n 1,$1p /root/tmp/workfile > /root/tmp/temp.root
crontab /root/tmp/temp.root -u root
cat /var/spool/cron/crontabs/root


I added the third line after my post so I could check to see what actually was loaded.

I use ctl-c in pico to determine how many lines I want to pass to the script.

scasey 07-16-2017 02:26 PM

An alternative to take the first x lines to a new file?
Code:

head -x /root/tmp/workfile > /root/tmp/temp.root
Where x is the number of lines to copy.
Should work from the command line, or as in your script with $1 instead of a hard-code for x
(or have I misunderstood?)

RPN 07-16-2017 03:28 PM

You didn’t misunderstand and that is a good thought. You could even pass two line number locations as arguments and pipe the first results to “tail -y” based on the difference between the arguments to only use y number of lines from the middle. I was focused on finding a solution using sed because that was what was suggested. There are probably many different ways of accomplishing the same thing.

In truth, as frankbell indicated above, there is no way to do it as nicely as the “exit” in the middle of bash script. If I had realized that the actual crontab file is not only the input but also a reflection of what cron has loaded, I would have known not to even ask the question. At least this was a learning experience.

scasey 07-16-2017 06:39 PM

Quote:

Originally Posted by RPN (Post 5735739)
You didn’t misunderstand and that is a good thought. You could even pass two line number locations as arguments and pipe the first results to “tail -y” based on the difference between the arguments to only use y number of lines from the middle. I was focused on finding a solution using sed because that was what was suggested. There are probably many different ways of accomplishing the same thing.

In truth, as frankbell indicated above, there is no way to do it as nicely as the “exit” in the middle of bash script. If I had realized that the actual crontab file is not only the input but also a reflection of what cron has loaded, I would have known not to even ask the question. At least this was a learning experience.

Excellent observation. I learned more about sed from your post, BTW, so thanks for that.

Note, however, that crontab doesn't necessarily contain everything that cron is running. My server has directories
Code:

/etc/cron.d
/etc/cron.daily
/etc/cron.hourly
/etc/cron.weekly
/etc/cron.monthly
/etc/crontab


michaelk 07-16-2017 06:56 PM

Just a FYI, crontab only maintains crontab files for users which are usually saved to /var/spool/cron/crontabs.

The files in the /etc directory are typically system cron jobs.

scasey 07-16-2017 09:19 PM

Quote:

Originally Posted by michaelk (Post 5735784)
Just a FYI, crontab only maintains crontab files for users which are usually saved to /var/spool/cron/crontabs.

The files in the /etc directory are typically system cron jobs.

Aha! That's where root's crontab -l is !! Another mystery solved!! ;)

michaelk 07-16-2017 09:29 PM

Actually root has its own cron files that can be created etc. via crontab.

System wide cron jobs can be run by any user but typically as root. Some system cron jobs are logrotate and updatedb which is the database used by the locate command.


All times are GMT -5. The time now is 06:59 AM.