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

Ubuntu 14.04: crontab -e open a weird file

Since I've reboot my server now when I open my crontab file using
crontab -e
Vim is opening a file called:
/tmp/crontab.yWhf4u/crontab
Who is empty...
I would like to know what I did wrong and how I can fix my mistake, please?
My command to reboot the server was
shutdown -r now
crontab -e creates a temporary file that's a copy of your crontab. After you leave the editor, it rereads the temporary file and uses it to update your crontab. (Your actual crontab is in a file in a system directory that you won't have access to without root permissions. Don't try to edit that file directly.)
Apparently your crontab is empty. crontab -l dumps the contents of your crontab to standard output; if that produces no output, it will verify that you have an empty crontab.
You didn't say so, but I presume you thought you had a non-empty crontab. It's easy to clobber your crontab accidentally. For example, if you type crontab with no arguments, it will read a new crontab from standard input; if you then type ctrl-D, it will replace your existing crontab with an empty one.
There's not really a good way to restore your previous crontab. It might exist in a system backup.
In the future, I suggest keeping a copy of your crontab in a separate file, backed up in some way (I use a version control system), and use crontab filename to load the contents of that file into your crontab. That way if you accidentally clobber your crontab, you can easily reload it.

How to run a script hourly that requires password?

I want to call a script every hour which I was doing by using following commands:
Step 1. Connect to your cloud desktop using ssh.
Step 2. Run these commands:
% screen
% while true; do ./parseScript.sh; sleep 3600; done
Step 3. Close the window with the running command.
Step 4. (same as Step 1) Connect to your cloud desktop using ssh.
Step 5. Run this command: screen -R. You will get back to the session you left in step 3.
Now the problem is that in my script I have 1 command which has to be executed using sudo hence this will ask for password every time, Is there anyway to run this script every hour except manually?
You can configure sudo to do not require password for a certain combination of user and command.
For instance, if the username that needs to run the command is keshav and the command is at /sbin/someCommand:
First create a copy of your sudoers file, just in case
sudo cp /etc/sudoers /etc/sudoers.backup
Then edit the /etc/sudoers
sudo visudo
That command will open the /etc/sudoers file in your default editor. Use the editor to add this line
keshav ALL = NOPASSWD: /sbin/someCommand
Save the file and exit the editor.
If you followed the steps correctly your user keshav should now be able to run sudo /sbin/someCommand without a password prompt
You can use expect command. Example code
#!/bin/bash
spawn scp $p testuser#192.168.2.2:/home/testuser
expect "Password"
send "Pass#123\r"

yum reinstall on cron not working

I have a cron job the is run by the user called "taskmanager" every day and I need to reset a configuration every time.
the user taskmanager has NOPASSWD on /etc/sudoer
I also have a private repository where I made an RPM of my configuration. Whenever I ran the bash script manually the yum -y works flawlessly, however when the script is ran by cron the exit code is 1, manual execution's exit code returns 0.
/usr/bin/sudo /usr/bin/yum -y reinstall --disablerepo="*" --enablerepo=codevault project-configuration
echo $?
Your cronjob doesn;t have the environment set up correctly - probably a missing environment variable.
Tasks run in cronjobs do not pick up the environment of the user, just the permissions. So your user has permission to run the command (your sudoers file is probably fine), but some environment variable required for the command to run correctly is not set.
To figure out what, look at the output of cron - you probably have mail. Once you've identified the missing variable, you can set it in your cron file in various ways

Startup script run as a specific user Ubuntu

So I have a user, userA on Ubuntu. When the machine starts I want to add a script to /etc/rc0.d called startService
From inside of this script it will start several services using three scripts
startServiceA.sh
startServiceB.sh
startServiceC.sh
I'd like those three scripts to be started from userA, not root. How would I achieve this?
You can use commands like: su, sudo, runuser
Be sure to check the man pages.
This site might be able to help you also
http://www.cyberciti.biz/open-source/command-line-hacks/linux-run-command-as-different-user/
You can run commands inside your startup script with:
sudo -u <username> ....
Note: you will need to to preface every command in the file that you want to run as another user. I'd recommend making a variable at the top of your script like so:
SUDO="sudo -u <username>"
Then just do: $SUDO <command>

How can i run a sudo command in Bash script?

I want to run the following sample bash script which needs sudo password for a command
#!/bin/bash
kinit #needs sudo password
vi hello.txt
while running the above script it is asking for password.
How can i pass the username and password in the command itself or is there any better way i can skip passing my password in the script ?
So if you have access to your full system, you can change your sudoers file to allow certain sudo commands to be run w/o a password.
On the command line run visudo
Find your user and change the line to look something like this:
pi ALL=(ALL) NOPASSWD: /path/to/kinit, /path/to/another/command
That should do it. Give it another shot!
Hope that helps
TL;DR
You can't—at least, not the way you think.
Longer Answer with Alternatives
You have a couple of options:
Authenticate interactively with sudo before running your script, e.g. sudo -v. The credentials will be temporarily cached, giving you time to run your script.
Add a specific command such as /usr/lib/klibc/bin/kinit to your sudoers file with the NOPASSWD option. See sudoers(5) and and visudo(8) for syntax.
Use gksudo(1) or kdesu(1) with the appropriate keyring to cache your credentials if you're using a desktop environment.
One or more of these will definitely get you where you want to go—just not the way you wanted to get there.
You shouldn't pass username and password. This is not secure and it is not going to work if the password is changed.
You can use this:
gksudo kinit # This is going to open a dialog asking for the password.
#sudo kinit # or this if you want to type your password in the terminal
vi hello.txt
Or you can run your script under root. But note that vi is going to be ran as root as well, which means that it will probably create files that belong to root, that might be not what you want.

Resources