Export crontab to a file - cron

How can I export the contents of the crontab to a file?
I need it because I'm switching usernames and do not want to lose the data in the crontab.

Create the backup (export):
crontab -l > /some/shared/location/crontab.bak
Import it from the new user:
crontab /some/shared/location/crontab.bak

Related

Use crontab to update file in Perforce

I tried to Use crontab to update file in Perforce at every hour like this.
30 * * * * /proj/tester.sh
and the tester.sh was write like this:
#!/bin/bash
export P4USER=<myname>
export P4CLIENT=<myclient>
export P4PORT=<myport>
python3 test.py
p4 edit -c defalut /proj/testtext.txt
p4 submit -d "my test on p4 (by code)"
test.py add a new line to testtext.txt
It normally run good when I run it by terminal, but when I use crontab, It can't work as my expect.
I think that might environment variable cause the problem but I can't see the log of crontab to confirm it.
Also, I don't have root permission to change environment by /etc/profile.
What can I do to deal with it?

Create crontab for specific user

What is the most optimal approach for creating a crontab for a specific user, as part of a Packer build process? Much of what i've seen covers using the crontab utility as a text editor, and does not specify using pipes or output to load the file. This operation would be run from sudo, since the cron directories are root-owned directories.
The simplest way to "import" file in to cron record is to use command like this:
crontab input_file
the input file should contain records on the same way they are stored in cron files, something like
1 2 3 4 5 /path/to/executable
Assuming that you have your script copied to /home/ec2-user/scripts/test.sh, below one liner will setup the crontab to run the script. We use this in our Packer shell provisioner to setup the crontab entries. Below sample entry runs every minute. You can change the timing.
(crontab -l 2>/dev/null; echo "* * * * * /home/ec2-user/scripts/test.sh") | crontab -

Simple cron with flock not working on Ubuntu

I've created the file crontester on /etc/cron.d with this line on it:
* * * * * /usr/bin/flock -n /tmp/fcj.lockfile touch /tmp/test.txt
That should be running every minute.
But I dont see the /tmp/test.txt file being created, so the cron is not working correctly.
What I'm doing wrong? Do I've to create the /tmp/fcj.lockfile, if I've to do this, do I have to create it empty?
Thanks a lot.
The command runs fine on my machine, so the cronjob is probably not set up properly. man cron discourages creating /etc/cron.d/ files:
Like /etc/crontab, the files in the /etc/cron.d directory are monitored for changes. In general, the system administrator should not use /etc/cron.d/, but use the standard system crontab /etc/crontab.
Try creating the cronjob using crontab -e and see if it works

does $crontab -e eventually updates /etc/crontab?

I'm digging along CRON and scheduling.
I setup a scheduled job to fire every minute via $crontab -e + editing the file (weirdly named "/tmp/crontab.vst6TX/crontab")
My understanding is that $crontab -e opens A crontab... and that cron.d, the daemon, picks up the crontab and APPENDS the cron job within to the (systemwide) /etc/crontab. (as per comment from crontab being saved in tmp/ in debian)
I'm watching the cron job fire every minute - yet I can't see it being added to the /etc/crontab job list... why? $crontab -l does show the job...
crontab -e and crontab -l are to edit and display (respectively) the current user's crontab file (which are physically located in /var/spool/cron/crontabs). Therefore, each user can have their own separate crontab file in that directory. So when you ran crontab -e and added a cron line, you ran crontab -l as the same user presumably, and therefore saw the line you added.
/etc/crontab is a completely different file. You are correct, it is systemwide -- notice that the cron lines in that file specify a user. The same is true for files in /etc/cron.d, the cron lines in the files will specify a user.
Oh and also, the .d suffix in cron.d does not refer to daemon. Check this post out.

how to update crontab without using crontab -e

I want to update my user crontab from a java program. That is, I want to load the existing cron file, delete any obsolete lines, add new lines and then save the new file.
I'm not sure how to proceed with this. The actual cron file is in /var/spool/cron (centos) but there are warnings not to modify this file. crontab -e doesn't help unless I write a custom editor to do the file swap or a complicated shell script to pipe in the new lines after deleting the old file.
So, is there a simple way to write out a cron file and then install it?
The command crontab without any options installs a crontab from stdin.
crontab -l returns the current crontab if you need to append to the old crontab.

Resources