LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-14-2017, 09:52 PM   #1
RPN
LQ Newbie
 
Registered: Aug 2005
Distribution: Slackware
Posts: 18

Rep: Reputation: 5
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.
 
Old 07-14-2017, 10:01 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,310
Blog Entries: 28

Rep: Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136
I've never tried it, but I believe the short answer is "No."
 
Old 07-14-2017, 10:25 PM   #3
RPN
LQ Newbie
 
Registered: Aug 2005
Distribution: Slackware
Posts: 18

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by frankbell View Post
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.
 
Old 07-14-2017, 10:35 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
sed ?
 
Old 07-14-2017, 11:14 PM   #5
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,310
Blog Entries: 28

Rep: Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136
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.
 
Old 07-14-2017, 11:25 PM   #6
RPN
LQ Newbie
 
Registered: Aug 2005
Distribution: Slackware
Posts: 18

Original Poster
Rep: Reputation: 5
Quote:
Originally Posted by syg00 View Post
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.

Last edited by RPN; 07-14-2017 at 11:31 PM.
 
Old 07-14-2017, 11:30 PM   #7
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,310
Blog Entries: 28

Rep: Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136Reputation: 6136
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?
 
Old 07-15-2017, 12:25 AM   #8
RPN
LQ Newbie
 
Registered: Aug 2005
Distribution: Slackware
Posts: 18

Original Poster
Rep: Reputation: 5
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.

Last edited by RPN; 07-15-2017 at 11:04 AM. Reason: workfile and temp.root should not be in the crontabs directory
 
Old 07-16-2017, 02:26 PM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,723

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
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?)
 
Old 07-16-2017, 03:28 PM   #10
RPN
LQ Newbie
 
Registered: Aug 2005
Distribution: Slackware
Posts: 18

Original Poster
Rep: Reputation: 5
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.
 
Old 07-16-2017, 06:39 PM   #11
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,723

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Quote:
Originally Posted by RPN View Post
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
 
Old 07-16-2017, 06:56 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,675

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
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.
 
1 members found this post helpful.
Old 07-16-2017, 09:19 PM   #13
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,723

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Quote:
Originally Posted by michaelk View Post
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!!
 
Old 07-16-2017, 09:29 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,675

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: Monitoring Network Load With nload: Part 2 LXer Syndicated Linux News 0 11-23-2016 02:04 PM
LXer: Monitoring Network Load With nload: Part 1 LXer Syndicated Linux News 0 11-17-2016 12:12 AM
[SOLVED] crontab does not reread the crontab file upon modification of the latter. stf92 Slackware 4 08-01-2015 06:12 PM
crontab -e load a file into casperdaghost Linux - Newbie 1 07-24-2010 01:24 AM
How to send a html file as an file attachment to my mail from crontab? GRD Linux - Newbie 2 06-03-2008 11:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:23 AM.

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