I need to add a cron job thru a script I run to set up a server. I am currently using Ubuntu. I can use crontab -e but that will open an editor to edit the current crontab. I want to do this programmatically.
where the file named "filename" contains items to append. You could also do text manipulation using sed or another tool in place of cat. You should use the crontab command instead of directly modifying the file.
crontab script download
Download
https://t.co/vqpyCDD1uJ
If you are modifying or creating system crontabs, those may be manipulated as you would ordinary text files. They are stored in the /etc/cron.d, /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly directories and in the files /etc/crontab and /etc/anacrontab.
Joe Casadonte's one-liner is perfect, except if you run with set -e, i.e. if your script is set to fail on error, and if there are no cronjobs yet. In that case, the one-liner will NOT create the cronjob, but will NOT stop the script. The silent failure can be very misleading.
The reason is that crontab -l returns with a 1 return code, causing the subsequent command (the echo) not to be executed... thus the cronjob is not created. But since they are executed as a subprocess (because of the parenthesis) they don't stop the script.
(Interestingly, if you run the same command again, it will work: once you have executed crontab - once, crontab -l still outputs nothing, but it doesn't return an error anymore (you don't get the no crontab for message anymore). So the subsequent echo is executed and the crontab is created)
Crontab files are simply text files and as such can be treated like any other text file. The purpose of the crontab command is to make editing crontab files safer. When edited through this command, the file is checked for errors and only saved if there are none.
Therefore, a script can either directly write cron tab files, or write them to a temporary file and load them with the crontab [path to temp file] command. Writing directly saves having to write a temporary file, but it also avoids the safety check.
In Linux, the default location of the crontab file is /var/spool/cron/. Here you can find the crontab files of all users. You just need to append your cronjob entry to the respective user's file. In the above example, the root user's crontab file is getting appended with a cronjob to run /root/test.sh every day at 1 AM.
As a correction to those suggesting crontab -l crontab -: This does not work on every system. For example, I had to add a job to the root crontab on dozens of servers running an old version SUSE (don't ask why). Old SUSEs prepend comment lines to the output of crontab -l, making crontab -l crontab - non-idempotent (Debian recognizes this problem in the crontab manpage and patched its version of Vixie Cron to change the default behaviour of crontab -l).
EDITOR=cat tells crontab to use cat as an editor (not the usual default vi), which doesn't change the file, but instead copies it to stdout. This might still fail if crontab - expects input in a format different from what crontab -e outputs. Do not try to replace the final crontab - with crontab -e - it will not work.
I don't know that you need to define what user you want it to run as when its in crontab -- commands will be run as the user who makes the entries with crontab -e. To create a cron process that runs as root, either login as root or set it up with $ sudo crontab -e
I have a script that reminds me to restart my computer if uptime is more than, say 3 days (although its set to 0 days now just to check if the script is running as my computer has been up only over a day..).
Okay, so that works...what did we do?
We changed all the commands not to depend on paths we didn't explicitly set
We ran our script explicitly with bash
We told the script that we expect to be on DISPLAY :0.0
I've been trying different setups, but can't figure out what is wrong. I can run the script manually and everything goes perfectly, so I guess there is something wrong with my cronjob entry, but can't really understand what. Could you please help me figure it out? Thanks!
A common "gotcha" is the PATH environment variable being different. Maybe your cron script uses the command somecommand found in /opt/someApp/bin, which you've added to PATH in /etc/environment? cron does not read that file, so running somecommand from your script will fail when run with cron, but work when run in a terminal. To get around that, just set your own PATH variable at the top of the script.
I have an imaginably simple script to update Pi-hole, which I run once a month as root scheduled as a cronjob. I use a script instead of pihole -up in the crontab, because I also edit the lighttpd listening port after the update, which is irrelevant to the question.
The script is running fine from shell but not working in crontab.Several tests (touch testfile) showed that crontab is working and all scripts are executed. I verified that with a simple echo command at the beginning of the new script.
I currently run the UTM, but would like to go for the XG firewall, but i have a script running at startup on my UTM, whis is a "Wake-on-access" script which sends a magicpacket when a certain port is present in the firewall log.
Even if could be useful to schedule script inside the XG for security reason it should be denied to allow to install/import other packages/script than Sophos Official one. You could use XG API integration and interact with XG.
As John mentioned, it is a matter of your script being interpreted differently in the two environments (using /bin/sh under cron, and using your existing shell, which is probably /bin/bash when you source it in directly). Actually, /bin/sh is usually just a symlink to /bin/bash, and the bash executable behaves differently depending on the name under which it was invoked, but that's just an aside.
Based on the answer you've chosen to this question, you can see that invoking the script directly with /bin/bash will cause Bash to interpret your script, with no problems. For future reference, though, only use POSIX syntax in scripts that start with a #!/bin/sh shebang, and use #!/bin/bash for scripts that contain Bash-specific syntax.
this script works fine when I run it via ssh console, but if I add it as cron job in /etc/crontab, the script runs, creates the dump.sql, puts the archive in tmp folder, but does not upload it to azure. i am guessing it fails at the rclone part.
The usual problem people have with rclone in crontabs is it not being able to find its config file, so you can try adding the --config /path/to/config directive. Use rclone config file to show where it is.
the error comes on part number 3 (uploading to rclone), if i run this script as cron.
this is because the .tar files keep accumulating in /tmp folder but are not uploaded to azure when I put on the cron.
Thanks Rich! I had forgotten about visudo.
I ran the crontab as the user openhabian (without the sudo command). This should mean the script is run as openhabian, it seems.
I ran visudo and added the below line
If any of this is incorrect, please let me know.
I am still running into obstacles. Despite modifying the sudoers file, If I run my script (removing the sudo commands), the systemctl commands fail to run and throw errors.
Oddly enough, rsync gives errors in the script (run as sudo or not):
No worries thanks for getting back Ben, I do take full image backups when I hit milestones as and when. I have started playing with cron and getting backups off ship to my NAS successfully! I will be checking out your script as it looks very comprehensive.
I have a Dell R710, and I found this script ( -IPMI-TEMP/R710-IPMITemp.sh) online to automatically control fan speed via IPMI. I was trying to set this up on my server with a custom script. I had a couple of questions regarding this:
1) I originally created my own shell script based off the above example in /root of my UNRAID server. The script was working well, but I had to stop the array, and restart the server. When the system rebooted, I noticed that my script stored in the path /root was deleted. This was surprising to me as I had expected the root filesystem to be persistent. Is this not true? If it is, where is a recommended place to store this script?
2) I had manually created a crontab entry for the root user to the run the above script periodically. That crontab entry also didn't persist across reboots. I was wondering what's the recommended way (plugin) for doing the crontab entry?
Squid, thanks for the reply, and for the "user scripts" plugin - it's very useful. I've created the same script via the user scripts plugin, and everything works well. I'm looking for a way to capture the output of my script to a log file (preferably in the plugin folder or in /tmp/) to check if everything is running properly. I couldn't find a way when I searched the forum. Could you shed some light into how I'd do this? Thanks!
run (ipmitool -I lanplus -H $IPMIHOST -U $IPMIUSER -P $IPMIPW sensor list grep Temp) to get the list of what your temp sensors are called and replace "Ambient" with whatever sensor you want to run your script off of. then set a cron job for how often you want the script to run. this is set schedule to custom and in the bar that pops up put in the code of numbers need ( this may help -job ).
i am using this script and it works but in my dell r720 both cpus just come up as "temp" and the script is grabbing the lower of the 2 "temp" sensors. is there an edit to take the highest of the 2 sensors "temp"? sorry I've tried looking around and a few edits but cant figure it out any help would really be appreciated, thank you.
Hello, I have a script for backing up my files to a external drive and I want to do this every day at a certain time, I was using crontab as a standard user and it was working fine, I decided change my script so that I can mount and unmount the drive before and after running it, this required me to run the script as root, so I tried to seet up a crontab as root using crontab -e but it never runs and when I try: systemctl start cron the terminal outputs: Failed to start cron.service: Unit cron.service not found why is this and how do I fix it?
35fe9a5643