I am trying to create a program that will set up a crontab to run a program that checks the server's health. Is there a way to create a new crontab without any user input? I've tried a bunch of ambitious things mostly consisting of something along the lines of
$echo "* * * * * /example" >> ~/crontab
when I run this code I get a permission denied error, even when running it under a sudo call. I know a question similar to this has been asked on this website, but there was no functional answer. Is there a way to automate adding crontabs? Or is it necessary to use contab -e and add it via the text editor?
crontab is regular text file, so you can add entries like you tried. This is a permission issue. Either change crontab file permission or set caller script +s permission.
P.S. ~/crontab means file located in home directory. If you call as web user, home is located in /var/www
$ echo "* * * * * /example" > my-crontab
$ crontab my-crontab
Thanks to https://askubuntu.com/a/216711/911295
Related
I have a addlog.sh file which has the following content:
echo "time right now:" `date` >> ~/Desktop/date.log
This is how my crontab -l looks like:
* * * * * /bin/sh /Users/naman/Desktop/addlog.sh
I have added correct permissions to addlog.sh file and my shell is at /bin/sh.
I have verified that the manual run of addlog.sh is appending time to the date.log file.
I am running this in my local Mac. But its not working and not sure what I might be missing here.
I suspect you are running Catalina (10.15) with its restrictive system integrity protection. There are two possible solutions.
Solution 1
Move your script and log file out of ~/Desktop into ~/. In contrast to ~/Desktop, ~/Documents, and may other folders, the home directory is not subject to system integrity protection.
Solution 2
If you need to keep your script and logfile under ~/Desktop:
Add #!/bin/sh shebang (first line) to your addlog.sh script
Make a trivial addlog.app from your addlog.sh script as follows:
mkdir -p ~/Desktop/addlog.app/Resources/MacOS/
cp ~/Desktop/addlog.sh ~/Desktop/addlog.app/Resources/MacOS/addlog
chmod +x ~/Desktop/addlog.app/Resources/MacOS/addlog
Change your crontab to the following:
* * * * * /Users/naman/Desktop/addlog.app/Resources/MacOS/addlog
You also have to allow the newly created app access to files in SIP-protected areas, as follows:
Open 'System Preferences', 'Security & Privacy'
Click on the padlock and authenticate to make changes.
Select 'Full Disk Access' in the left pane.
On the right-hand side click +.
Navigate to Desktop and select your new app addlog. It will be added to the list of apps. Ensure that the checkmark is selected.
Close 'System Preferences'
I want to use cron for execute a script periodically. I want to try a simple script first but it does not work.
This is my script (scritp.sh) which permission are 700:
#!/bin/sh
clear
echo "Hello!"
mkdir Hello
And this is the crontab file when I edit it with the command crontab -e:
SHELL=/bin/sh
* * * * * /home/padro/Documents/script.sh
EDIT:
I have that script on /home/padro/Documents folder. What I do after it is execute the command crontab -e for modify the cron file. In this file I put the shell that I want SHELL=/bin/sh and also the cron schedule expression * * * * * /home/padro/Documents/script.sh. This schedule teorically run the script every minute. Finally I save the file and when a minute passes I can't see the echo of the script on the terminal.
EDIT2:
I have added mkdir hello, because I don't know if the echo of the script is shown on the terminal. But the hello directory is never created.
Any output generated by a program called from cron will by default be emailed to the user owning the crontab (assuming local delivery of mail messages is possible). So I'd suggest that you look in your inbox on the local machine.
To save the output into a file, use a redirection in the crontab, or arrange for the script to write its output to a file.
Jobs started by cron does not run with a terminal, so you should not expect to see your terminal being cleared every minute by running this script through cron.
The Hello folder should have been created in the working directory used by the script (possibly your home directory). To make absolutely sure you know where the script's working directory is, use cd in the script to move to the correct location.
I do not have enough reputation to add comment.
My humble comment would be.
Is the cron file you mentioned via root?
cos chmod 700 a file would be only be executed by owner.
If you are using redhat linux, the user account you use on the first log in is user rights NOT root.
Reference link to a cheat sheet.
su - root
system will prompt root password
crontab -e
* * * * * /home/padro/Documents/script.sh
You can even run a test script, which I did encounter the similar situation as you when I first learnt scripting into your crontab-
* * * * * date > export/home/padro/Documents/testing.txt
If you could, restart the server.
Check if your directory is correct using the command
pwd in linux/unix.
I hope my comment based on my recent learning have helped you.
Edit 1: Remove clear in your script. Thanks...
Edit 2: I believe your Hello folder is created at the core of the root folder try looking for it... or the home directory of the user...
I have an issue related to cron.
Suppose in a shell script,i'm running a query and the out put is getting stored to a temporary file. What is the location of that temporary file? and how can i delete in the shell script itself?
If a command cron is running has output (e.g. status reports, or
errors), cron will email the output to whoever is specified in the
MAILTO variable. If no one if specified, then the output will be
mailed to the owner of the process that produced the output.
From here
output.txt will end up in the directory the cron starts your command with. Unfortunately, the POSIX crontab specification does not seem to specify what that directory is. Neither could I find anything on that in the man pages on my system.
On my system cron will use the home directory of the user it runs the command as. If you don't find output.txt there on your system, you can tease the directory out of cron by temporarily adding a line like the following to the crontab:
For user crontabs:
* * * * * pwd >>/tmp/cronpwd
For the system crontab (replace user by the name of the user that ran the original command):
* * * * * user pwd >>/tmp/cronpwd
Then wait a minute, and the file /tmp/cronpwd should appear, containing the name of the current directory of cron-commands. Don't forget to remove the extra line from the crontab again!
Whatever the directory is, check that it is actually writable by the user running the script. If it isn't then the output is lost and there is no way to get it back.
Note that it is probably a good idea to simply make the command independent of the current working directory, either by specifying absolute paths or by explicitly changing the current directory at the beginning of the command:
5 0 * * * /path/to/your/script.sh > /where/it/should/go/output.txt
5 0 * * * cd /where/it/should/go && /path/to/your/script.sh > output.txt
(That example is a user's crontab, not the system crontab.)
Hi I'm a first year game programing student learning Unix Bash, I have run into a problem trying to understand crontab. I'm trying to do some rather simple things, checking to see if I am online, getting information about a given website, and ping another website to verify it is online. My script file does all of this without fail, however when I try to perform these tasks through crontab I get emails telling me absolutely nothing but jibberish. The output basically just tells me that I am trying to do all these things, but it doesn't output the results. I'm not sure where I am going wrong.
Just to verify I do have permission on the system to use crontab, and I have the script running every minute while I am trying to get it working. I'm hoping someone can point me in the right direction, all of my research online has really just led me astray.
This is my crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
HOME=/
LOGNAME=username
* * * * * /bin/bash /home/students/~/online.sh
30 23 1 * * rm online.log
this is my script
touch online.log
who | grep username >> online.log ; whois yahoo.ca >> online.log ; ping -c 1 www.google.com >> online.log
You need to use absolute paths in your scripts if you want to execute those using cron.
Note that cron executes in a different environment from what you get while executing a script on the command line. For example, changes lines like
touch online.log
to include the absolute path to online.log.
The output is being redirected into online.log, so you need to look there, not in your emails. If you want the output to be in the emails as well, you should look into using tee instead of a redirection.
I am trying to to run a cronjob of a script on an EC2 Instance but it's simply not working
The way I am running it right now is
15 * * * * root /home/ec2-user s3_upload.sh TestBucket
I wrote this when I did crontab -e
Right now I am in this directory /home/ec2-user/ so I don't know if maybe that has something to do with why it's not running.
I just don't understand this whole cron thing and how permissions work.
Any help is greatly appreciated!
You can't specify a username in a user's crontab, only in /etc/crontab.
You can't specify a directory in any form of crontab. To change directory, use cd.
The environment is not the same. If you e.g. set PATH in .bashrc, these will not be automatically included, so it's safest to use full paths.
As root (assuming you want to run this as root and not ec2-user or something), run crontab -e and add
15 * * * * cd /home/ec2-user && /full/path/to/s3_upload.sh TestBucket
Make sure you can copy-paste the command in a root terminal and get it working, and read root's mail afterwards to see the command's output with possible error messages. If it still doesn't work, make sure you have these errors available.