OS X Shell Script to run ntpdate - bash

turns out I'm really bad at shell scripts...
I'm trying to make a script that will run;
ntpdate -u time.apple.com
So I created this script;
#!/bin/sh
ntpdate -u time.apple.com
I've chmod +X the script but it won't execute when I call
sudo ./timefix.sh
sudo: ./timefix.sh: command not found
If I just call ./timefix.sh I get;
-bash: ./timefix.sh: Permission denied
ntpdate requires sudo privileges to run.
My final goal is to package this up into an app with Automator so that it runs every boot. I have a handful of Macs in this office that are having internal clock issues. This is meant to be my dirty work around until I can get time to strip them back and replace the RTC batteries.
Can anyone help me fix my script please?

just change +X to a lower case +x.
chmod +x ./timefix.sh

Related

Why does following command work in the bash but not from script

I'm experiencing this really weird issue which I just can't understand why it is happening. When I execute following command from my shell manually it works.
sudo -u some-user echo "$SSH_KEY" | /home/some-user/.ssh/authorized_keys
however from a bash script it fails with a message we don't have any permissions.
#!/bin/bash
sudo -u some-user echo "$SSH_KEY" | /home/some-user/.ssh/authorized_keys
Is there any bash option to configure, or can someone explain this behavior?
It looks a bit like a bash security thingy for non interactive terminals or something like that, but I lost my creativity to google for the solution.
I'm running Ubuntu 16.04.
After hours of investigation it seems sudo commands where not executed because of the password prompt which does not occur from the script.
The solution was to first run a sudo command before runnning the script or just runnning the whole script as sudo so you can enter the password one time and the other sudo commands are running without password prompts.

bash reboot command not found

I am trying to execute a script on server. When I do crontab -l I get #reboot cd my_project_path; ./start.sh on terminal.
However when I do #reboot cd my_project_path; ./start.sh I get -bash: #reboot: command not found. How do I include reboot command in bash? Sorry if this is a very basic question, but I am not master in bash.
There isn't a command #reboot. I think you're looking for
shutdown -r now
or (possibly)
/sbin/reboot
which will reboot your machine. However, in crontab a #reboot is a scheduled time, so that's the command it would run when your system has just rebooted... so perhaps you really just wanted
cd my_project_path; ./start.sh
#reboot in crontab means "Do that / execute that on server boot"

Remove sudo permissions for one command

this is probably a really simple question. I apologize if it is a duplicate.
I want to know how to remove sudo permissions for one particular command. I've created a script that installs a bunch of .deb packages and it needs sudo to do that, but one command in it needs to run without sudo permissions, so how would I do that? I'm using Ubuntu and this is a bash script.
I'm calling my script: ROS_install
Here is part of the script:
sudo dpkg -i /home/forklift/Desktop/ROS/ros-hydro-laser-proc_0.1.3-0precise-20131015-2054-+0000_amd64.deb
sudo dpkg -i /home/forklift/Desktop/ROS/ros-hydro-urg-c_1.0.403-0precise-20131010-0128-+0000_amd64.deb
sudo rosdep init
sleep 2
rosdep update
The command "rosdep update" needs to be run without sudo permissions. I assumed that it was already, but I get a warning every time I run the script, and thus get locked out of the command after installation.
Rather than give the entire script elevated privileges, just give them to the actual commands that need them. That is, rather than
$ sudo my_script
modify my_script to use sudo only on those commands that need it. For instance, if this is your script:
command1
command2
command3
command4
command5
and command3 is the non-sudo command, modify your script to read
sudo command1
sudo command2
command3
sudo command4
sudo command5
In the process, think about whether command1 actually needs to run with sudo, or it it can run just as well without. In that way, you should be able to greatly reduce the number of commands that actually need to be run with sudo in your script.
If your command is running with full privileges, it also has the privilege to demote its own privileges, for good or for the duration of one command, by running su.
touch /privileged
su -c 'cp /privileged /tmp/not' nobody
I assume you are calling your script like:
sudo script.sh
And you do not want all of the commands within the script to run as root.
If your script is like:
apt-get install perl
apt-get install python
mv trash /home/user/
And you only want to run the first two commands as root you can specify a specific user for the third like:
su -c "mv trash /home/user/" user
Where user is the username you want to run the command as.
This will allow you to make a single sudo call at the parent level when you call the script.
If you don't want the username hardcoded, you can use a command like logname to get the username of the user that you are logged in as.
Just adding to the other answers, you can do this:
su -c "command" $SUDO_USER
Which will execute the command as the actual user who typed the sudo command
That's very useful when you are making scripts that require sudo to install something and write something in the user's $HOME

Execute single command in shell script without sudo

I have a simple shell script that is run with sudo as most of the script requires it, yet one of the commands in the script is a Homebrew install, which cannot be executed with sudo..
So, my question is when executing a shell script with sudo how do I execute sub commands as the current user and then continue the remainder of the script with sudo.
Prompting the user to enter his password again is not really practical as the script takes really long to execute and would require waiting 5-10 min for the prompt.
The easiest way is to run the subcommand via sudo from within the script. The user id to run with can be obtained by $SUDO_USER (look at the output of sudo env):
sudo -u $SUDO_USER ./exec_as_normal_user.sh
Instantiate the shell using
sudo -u $USER_NAME bash
and execute the shell script by calling,
./program.sh

Running individual commands works but not when combines in a shell script.. Why?

I need to run the following set of commands in a shell script
modprobe nbd
sudo qemu-nbd -c /dev/nbd0 path/to/image/file
sudo mount /dev/nbd0p1 /mnt/temp
python copyFiles.py
sudo umount /mnt/temp
sudo qemu-nbd -d /dev/nbd0
sudo rmmod nbd
When I individually run these commands it works fine, but when I put them in a shell script and executed that shell script, I always end up with an error in the mount command.
So I threw in a sleep 1 before mount and it works as expected.
What could be the reason behind this?
(Some sort of asynchronous call registration delay/ race condition?)
mount error: mount point /mnt/temp does not exist
So it seems the directory /mnt/temp doesn't exist when you are running it as a shell script. Just create it or add this in your script somewhere before the mount command:
mkdir /mnt/temp 2>&1 /dev/null
Both mount and the previous command require escalated privileges. Does it error cause the lock is still in place from the previous command when mount tries to run?

Resources