Editing crontab using a script - bash

I am trying to write an embedded application upgrade script. I am running my application on Ubuntu on Beaglebone Black.
Right not, I am launching my application on power-up. To do this, I am running launch.sh script in crontab.
This script is as mentioned below:
until /root/aa_main; do
echo "Application aa_main crashed with exit code $?. Respawning.." >&2
echo "Crashed! $(date)" >> crashlog.txt
sudo sync
sudo reboot
done
It basically, reboots the system if my application crashes, and crontab launches the application again on reboot.
The problem is that if I want to update the application. I have to perform following steps:
Disable launching of the above script with crontab -e
Reboot
Copy and replace aa_main
Enable launching of the same script with crontab -e
Reboot
I want to automate this process. How can I do it?
Is it possible to edit crontab using a script?
Thanks in advance

You can use crontab command's other options to copy the crontab to a file, modify it and install the modify version from a script:
crontab -l would list the current crontab file, you can redirect this to a file of your choice and modify it
crontab <filename> would install the file specified by <filename> as the new crontab - use this option with your modified file and you'll get the new modified crontab

Related

`notify-send` works when invoking script manually, but not from a crontab

I wanted my cron job to report to me on desktop when it executes, through notify-send command on Ubuntu. I've read through the common problems that stated a shell script didn't have access to a display, which is solved by adding this before calling notify-send:
export DISPLAY=:0.0
So i am okay in that regard.
The place where i am right now, is that my script works and notifies me on desktop if i invoke it from terminal manually, but not from the crontab.
The situation is as follows:
The script that executes is a PHP file. The PHP command to invoke the shell command, is:
<?php
`export DISPLAY=:0.0 && command -v notify-send && notify-send "Hello world"`;
(backticks in PHP mean execute in shell)
In both cases i am running it as root
When testing from terminal, i run:
sudo -u root /usr/bin/php -q /var/www/html/cron.php &> /dev/null
This works, and i get a desktop notification
To edit my crontab for the root user, i use:
sudo -u root crontab -e
In my crontab file, my line is this:
* * * * * /usr/bin/php -q /var/www/html/cron.php &> /dev/null
This one does not produce a desktop notification, even though the script 100% executes (i have the successful result in log files).
What goes wrong here, and why wouldn't i get the desktop notification?
you must set the PATH within the script or export it from crontab!
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

How to run crontab for first time with no user intervention

I am attempting to run the command below in an installation script. The first time this command runs, it prompts me to select an editor and then I have to save the file manually. Here is the command:
crontab -u www-data -e
How do I make it so that I don't need user input at all?
It should just take default: nano, saves the file, & exists nano.
You can edit the cron file directly -->
/var/spool/cron/crontabs/your_user
Then restart cron gracefully
sudo service cron reload
or even just a crontab edit
crontab -e

Run an shell script on startup (not login) on Ubuntu 14.04

I have a build server. I'm using the Azure Build Agent script. It's a shell script that will run continuously while the server is up. Problem is that I cannot seem to get it to run on startup. I've tried /etc/init.d and /etc/rc.local and the agent is not being run. Nothing concerning the build agent in the boot logs.
For /etc/init.d I created the script agent.sh which contains:
#!/bin/bash
sh ~/agent/run.sh
Gave it the proper permissions chmod 755 agent.shand moved it to /etc/init.d.
and for /etc/rc.local, I just appended the following
sh ~/agent/run.sh &
before exit 0.
What am I doing wrong?
EDIT: added examples.
EDIT 2: Just noticed that the init.d README says that shell scripts need to start with #!/bin/sh and not #!/bin/bash. Also used absolute path, but no change.
FINAL EDIT: As #ewrammer suggested, I used cron and it worked. crontab -e and then #reboot /home/user/agent/run.sh.
It is hard to see what is wrong if you are not posting what you have done, but why not add it as a cron job with #reboot as pattern? Then cron will run the script every time the computer starts.
Just in case, using a supervisor could be a good idea, In Ubuntu 14 you don't have systemd but you can choose from others https://en.wikipedia.org/wiki/Process_supervision.
If using immortal, after installing it, you just need to create a run.yml file in /etc/immortal with something like:
cmd: /path/to/command
log:
file: /var/log/command.log
This will start your script/command on every start, besides ensuring your script/app is always up and running.

script working manually but not via crontab?

Wondered if anyone had any idea why the following problem is occurring, or had any tips where to look…I can run the shell script manually in ssh, but if I set it up to run in crontab i get the problems below.
Server is: FreeBSD 8, and I have access to all root permissions
I have a shell script (Bourne) that runs under the “root” permissions using crontab with the following command:
* * * * * /data/backups/scripts/server_log_check.sh > /data/backups/logs/cron_logs/server_log_check.sh_cron.log
The “server_log_check.sh” script checks to see if “the report server” is running with this command:
if ps -xauww | grep -v grep | grep java | grep www > /dev/null
then
#“reports are running, no need to try to restart it”
Else
/usr/local/etc/rc.d/tomcat55 start #start report server because it is not running
Fi
The problem is occurring on this line: “/usr/local/etc/rc.d/tomcat55 start”, when the script is run using crontab, but if I run the script manually via ssh this line of code runs without a problem, but all the rest of the code in the script executes fine, just not this line. Allternatively, if I paste this line /usr/local/etc/rc.d/tomcat55 start into the ssh command prompt, it runs just fine too.
I changed the “server_log_check.sh” ownership to be “root”, but that didn’t make a difference, and the script "tomcat55" ownership is "www". The crontab entry is being made under the "Root" profile, so, I assumed there is no problem running a file that is owned by a lessor permission such as "www" has
Do you have any ideas why cron is doing this?
Thanks in advance
Try adding the following which will add the error to the log file as well:
* * * * * /data/backups/scripts/server_log_check.sh > /data/backups/logs/cron_logs/server_log_check.sh_cron.log 2>&1
Also change this:
/usr/local/etc/rc.d/tomcat55 start
to:
cd /home/root
nohup /usr/local/etc/rc.d/tomcat55 start &
This should create a nohup.out in /home/root.

Cron job does not start [duplicate]

This question already has answers here:
CronJob not running
(19 answers)
Closed last month.
I have a cron job that I want to execute every 5 minutes:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /scr_temp/scheduleSpider.sh
In /var/spool/cron/crontabs/root
The cron should execute a shell script:
#!/bin/sh
if [ ! -f "sync.txt" ]; then
touch "sync.txt"
chmod 777 /scr_temp
curl someLink
fi
That works fine from command line but not from cron. However the cron itself is startet but the script does not start.
I read about the path problem but I dont really understand it. I setup a cron that writes some env data to a file. This is the output:
HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
SHELL=/bin/sh
If I execute the env command in command line I get following output for PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
What path do I have to set in my shell script?
Your $PATH is fine; leave it alone. On Ubuntu, all the commands you're invoking (touch, chmod, curl) are in /bin and/or /usr/bin.
How did you set up the cron job? Did you run crontab some-file as root?
It seems that /etc/crontab is the usual mechanism for running cron commands as root. On my Ubuntu system, sudo crontab -l says no crontab for root. Running crontab as root, as you would for any non-root account, should be ok, but you might consider using /etc/crontab instead. Note that it uses a different syntax than an ordinary crontab, as explained in the comments at the top of /etc/crontab:
$ head -5 /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Run sudo crontab -l. Does it show your command?
Temporarily modify your script so it always produces some visible output. For example, add the following right after the #!/bin/sh:
echo "Running scheduleSpider.sh at \`date\`" >> /tmp/scheduleSpider.sh.log
and see what's in /tmp/scheduleSpider.sh.log after a few minutes. (You can set the command to run every minute so you don't have to wait as long for results.) If that works (it should), you can add more echo commands to your script to see in detail what it's doing.
It looks like your script is designed to run only once; it creates the sync.txt file to prevent it from running again. That could be the root (ahem) of your problem. What that your intent? Did you mean to delete sync.txt after running the command, and just forgot to do it?
root's home directory on Ubuntu is /root. The first time your script runs, it should create /root/sync.txt. Does that file exist? If so, how old is it?
Note that curl someLink (assuming someLink is a valid URL) will just dump the content from the specified link to standard output. Was that your intent (it will show up as e-mail to root? Or did you just not show us the entire command?
First: you can substitute the first field with */5 (see man 5 crontab)
Second: have cron mail the output to your email address by entering MAILTO=your#email.address in your crontab. If the script has any output, it'll be mailed. Instead of that, you may have a local mailbox in which you can find the cron output (usually $MAIL).
A better syntax for you CRON is
*/5 * * * * /scr_temp/scheduleSpider.sh
Also, check the authority of your scheduleSpider.sh file. Cron runs under a different user than the one you are likely executing your program interactively, so it may be that cron does not have authority. Try chmod 777 for now, just to check.
I suggest to:
check that /scr_temp/scheduleSpider.sh has executable bit
set PATH properly inside your script or use absolute path to command (/bin/touch instead of touch)
specify absolute path to sync.txt file (or calculate it relatively to script)
Have you added the comand via crontab -e or just by editing the crontab file? You should use crontab -e to get it correctly updated.
Set the working directory in the cron script, it probably doesn't execute the things where you think it should.
You should add /bin/sh before the absolute path of your script.
*/5 * * * * /bin/sh /scr_temp/scheduleSpider.sh

Resources