Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
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.
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.
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?
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?)
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.