How to run crontab for first time with no user intervention - bash

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

Related

Run shutdown command inside bash script

I'm trying to make an executable file (bash script) to show me a notification and shutdown my computer when a process is not found.
I will run the script as a Startup Application and I'm using the notify-send and shutdown commands in this script.
The problem is:
(1) If I add myfolder/myscript to the Startup Applications list it can't run the shutdown command (root password is required for this)
(2) If I add the script sudo myfolder/myscript it can't show the notifications via notify-send application.
I've already done a lot of searching around the internet and tried these steps:
(1) Added the script path or /sbin/shutdown to the sudores via sudo visudo
(2) Added su - $USER -c "DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus before notify-send command (I found some users reporting that root can't send notifications)`
So... none of them worked. What I'm missing?
What can be done to display notifications AND shutdown?
Here is my code:
#!/bin/bash
#Search for a specific process and sleep if it is found (removed for space saving)
shut_time=$(date --date='10 minutes' +"%T")
notify-send -t 600000 "WARNING:
Program is not running.
Shutting down in 10 minutes (scheduled for $shut_time)."
#ALREADY TESTED BELLOW LINES (DON'T WORK)
#su - $USER -c "DISPLAY=$DISPLAY DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus notify-send -t 600000 'WARNING:
#Program is not running.
#Shutting down in 10 minutes.'"
sudo /sbin/shutdown -h +10 #Tried with our without sudo
I'm running MX Linux 18 (xfce, Debian based).
To execute a terminal or any commands even another bash script within a BASH SCRIPT, all you have to do is simply start with a dollar sign and enclose the whole commands and arguments if any with PARENTHESES as follows.
$(COMMANDS)
In his case, it would be
$(sudo shutdown 10)
The statement above will EXECUTE the SHUTDOWN command for 10 minutes system shutdown and spit out the actual date and time the system will be automatically shutdown just like you would run this command in a console. There is no need to turn the user into sudoer or superuser. Whenever he runs his bash script as a ROOT USER or using SUDO, he will be PROMPTED to enter the root password. That's all he has to do and the above command will be executed.
Plus, if there is ever a need to capture the output of any command or script, do as follow.
my_shuttime=$(sudo shutdown 10)
I think it lacks an entry for shutdown in the sudoers. Please create a file sudo under /etc/sudoers.d and make the following entry:
[YOURUSER] ALL = (ALL) NOPASSWD: /sbin/shutdown
Replace [YOURUSER] with your user account!

Editing crontab using a script

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

Creating crontab for non-root user

I am attempting to run a script through crontab that is required to run as an oracle user. I have tried creating a crontab for that user by:
su -u oracle crontab -e which has allowed me to create one. I edited the file to run a perl script:
0 5 * * * /usr/bin/perl /path/master.pl > /tmp/debug.log
However when the time passes nothing is run.
Is this the proper way to create a crontab for non-root user? Also the master.pl file call multiple scripts that also need to be done as a oracle user if that makes a difference.
Use crontab's -u option. The man page says:
-u Appends the name of the user whose crontab is to be modified.
If this option is not used, crontab examines "your" crontab,
i.e., the crontab of the person executing the command. Note
that su(8) may confuse crontab, thus, when executing commands
under su(8) you should always use the -u option. If no
crontab exists for a particular user, it is created for him
the first time the crontab -u command is used under his
username.
So the correct command is:
sudo crontab -e -u oracle

Run commands via Jenkins?

I have a script on remote Ubuntu server. I trying to execute the script after the jenkins build is succeeded, But the error says like this:
sudo: no tty present and no askpass program specified
The configuration is given below,
Can anyone help me?
Thank You.
The problem is that your script uses sudo at some point. The usual way around is to add the script that requires you to use sudo to the sudoers.
Example: in your script you use sudo service apache2 reload, now create a bash script containing that line and add that script to the sudoers file.
New script name: /home/quaser/restart-apache.sh
Use: visudo
Add at bottom of the file:
jenkins ALL=(ALL) NOPASSWD: /home/quaser/restart-apache.sh
Now, in your script change: sudo service apache restart to sudo /home/quaser/restart-apache.sh and you should not be asked for a password.
I had the same problem, I solved that by commenting Defaults requiretty on /etc/sudoers
cat /etc/sudoers| grep tty
#Defaults requiretty
From the man page:
man sudoers | grep requiretty -A 5
requiretty If set, sudo will only run when the user is logged in
to a real tty. When this flag is set, sudo can only be
run from a login session and not via other means such
as cron(8) or cgi-bin scripts. This flag is off by
default.

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