pbrun rm command not working in shell script - shell

I am new to scripting part. I have a cron job running which trigger a shell script file. Inside the file I have below command to remove the file
pbrun rm -f {some file location}
I have to use pbrun because file is having the required permission for root (which I don't have) and for pbrun. Manually running the same command deletes the desired file but running it through script it is not working.

Related

Bash Script Cant Write To Log Files

I've created a simple bash script that grabs some data and then outputs it to a log file. When I run the script without sudo it fails to write to the logs and says they are write-protected. It then ask me if it should unwrite-protect them, but this fails (permission denied).
If I run the script as sudo it appears to work without issue. How can I set these log file to be available to the script?
cd /home/pi/scripts/powermonitor/
python /home/pi/powermonitor/plugpower.py > plug.log
echo -e "$(sed '1d' /home/pi/scripts/powermonitor/plug.log)\n" > plug.log
sed 's/^.\{139\}//' plug.log > plug1.log
rm plug.log
grep -o -E '[0-9]+' plug1.log > plug.log
rm plug1.log
sed -n '1p' plug.log > plug1.log
rm plug.log
perl -pe '
I was being dumb. I just needed to set the write permissions on the log files.
The ability to write a file depends on the file permissions that have been assigned to that file or, if the file does not exist but you want to create a new file, then the permissions on the directory in which you want to write the file. If you use sudo, then you are temporarily becoming the root user, and the root user can read/write/execute any file at all without restriction.
If you run your script first using sudo and the script ends up creating a file, that file is probably going to be owned by the root user and will not be writable by your typical user. If you run your script without using sudo, then it's going to run under the username you used to connect to the machine and that user will need to have permission to write the log files.
You can change the ownership and permissions of directories and files by using the chown, chmod, chgrp commands. If you want to always run your script as sudo, then you don't have much to worry about. If you want to run these commands without sudo, that means you're running them as some other user and you will need to grant write permission to that user, whoever it is, in order to write the files/folders where the log files get written.
For instance, if I wanted to run the script as user sneakyimp and wanted the files written to /home/sneakyimp/logs/ then I'd need to make sure that directory was writable by sneakyimp:
sudo chown -R sneakyimp:sneakyimp /home/sneakyimp/logs
This command changes ownership of that directory and its contents to the user sneakyimp. You might also need to run some chmod commands to make sure they are writable by owner.

Execute symfony command in bash script

I can't get to execute symfony command in bash script when I run it in cron.
When I execute the .sh script by hand everything is working fine.
in my bash file the command is executed like this:
/usr/bin/php -q /var/www/pww24/bin/console pww24:import asari $office > /dev/null
I run the scripts from root, the cron is set to root as well. For the test i set files permissions to 777 and added +x for execution.
the bash script executes fine. It acts like it's skipping the command but from logs i can see that the code is executed
It turned out that symfony system variables that I have stored on server are not enough. When you start to execute the command from command line its fine, but when using Cron you need them in .env file. Turned out that in the proces of countinous integrations I only got .env.dist file and I've to make the .env file anyways.
Additionaly I've added two lines to cron:
PATH=~/bin:/usr/bin/:/bin
SHELL=/bin/bash
and run my command like this from the bash file:
sudo /usr/bin/php -q /var/www/pww24/bin/console pww24:import asari $office > /dev/null

EC2 User Data Script .sh file and Manual Execute Differ

I am trying to execute the following user data script
sudo wget https://files.mysite.com/downloads/myFile.tar -P /opt
sudo tar -xvf /opt/myTar.tar -C /opt
sudo /opt/myFile.sh
When I execute the .sh file manually I see this:
Extracting...
Unpacking...
Cleaning up...
Complete
and it creates a directory in /opt/myDirectory
I do see the console in /var/log/cloud-init-output.sh but it doesn't seem to create the directory when run as part of the userdata script.
You're calling a shell script within a shell script, so you need to make sure:
have this as the first line of your ec2 user data script #!/bin/bash
call myFile.sh with the source command (alias is .) like this: . /opt/myFile.sh so it will run the myFile script
Note: the ec2 user data script runs as root so you do not need to have sudo each time you run a command.
Solution: CD into the directory first.
#!/bin/bash
cd /opt
wget https://example.com/myTAR.tar
tar -xvf myTAR.tar
/opt/mySH.sh

Bash script failing when run by cron - mktemp outputting nothing

I have a shell script, that works when I run it manually, but silently fails when run via cron. I've trimmed it down to a very minimal example:
#!/usr/bin/env bash
echo "HERE:"
echo $(mktemp tmp.XXXXXXXXXX)
If I run that from the command line, it outputs HERE: and a new temporary filename.
But if I run it from a cron file like this, I only get HERE: followed by an empty line:
SHELL=/bin/bash
HOME=/
MAILTO=”me#example.com”
0 5 * * * /home/phil/test.sh > /home/phil/cron.log
What's the difference? I've also tried using /bin/mktemp, but no change.
The problem is that the script tries to create the temporary file in root directory when it is started from cron and it has no permission to do that.
The cron configuration file contains HOME=/. The current directory is / when the script starts. And the template passed to mktemp contains file name only so mktemp tries to create the temporary file in current directory and it is /.
$ HOME=/
$ cd
$ mktemp tmp.XXXXXXXXXX
mktemp: failed to create file via template ‘tmp.XXXXXXXXXX’: Permission denied

How do you execute a command on a remote system insde a BASH script?

As part of an intricate BASH script, I'd like to execute a command on a remote system from within the script itself.
Right now, I run the script which tailors files for the remote system and uploads them, then through a ssh login I execute a single command.
So for full marks:
How do I log into the remote system from the bash script (i.e. pass the credentials in non-interactively)?
How can I execute a command (specifically "chmod 755 /go && /go") from within the script?
Following Tim Post's answer:
Setup public keys and then you can do the following:
#!/bin/bash
ssh user#host "chmod 755 /go && /go"

Resources