How do i execute this cron job from a mac? - bash

I have a bash script that I can execute with cd ~/Documents/Code/shhh/ && ./testy if i'm in any directory on my computer and that successfully pushes to github which is what i want.
I'm trying to schedule a cron job to do this daily so I ran crontab -e which opens a nano editor and then I put 30 20 * * * cd ~/Documents/Code/shhh/ && ./testy to run daily at 10:30pm and hit control O, enter and control X. But still it it didn't execute. When I type crontab -l it shows my command & I have a You have new mail. message when I open a new window. Still my command doesn't execute even though it will when I run it from any other directory.
I think my crontab job is at /var/at/tmp so I ran 30 20 * * * cd ../../../Users/squirrel/Documents/Code/shhh/ && ./testy but still nothing, even though it does work when I write it out myself from that directory. Sidenote, I can't enter into the tmp folder even after using sudo
OK When I type in mail I see a lot message and inside i get this error
---------------Checking Status of 2---------------
[master 0c1fff8] hardyharhar
1 file changed, 1 insertion(+), 1 deletion(-)
fatal: could not read Username for 'https://github.com': Device not configured

When you by default open nano Input_file it opens it up in INSERT mode(unlike vi where we have to explicitly go to INSERT mode by pressing i key). Now when you have done CTNRL+O it will then ask you if you want to save changes to opened file or not eg--> File Name to Write: Input_file If you press ENTER then it will save it and will be back on the screen(to your Input_file) where you entered new line. Now you could press CONTRL+X to come out of Input_file. May be you are stuck after saving it and want to come out then try this out once?

crontab -e does not edit the crontab file "live" at all -- changes are saved to the active file only after you save changes and exit the editor.
It also sounds like you may be using incorrect directory paths. The command in a crontab entry will generally be executed starting from the user's home directory. So if your home directory is /Users/squirrl, the command cd ../parent_directory/ will try to move to /Users/parent_directory. I suspect this is not what you want.
Finally, note that cron jobs run with a very minimal environment, without running most of your usual shell setup files (e.g. .bashrc). Most notably, if your script uses any commands that aren't in /bin or /usr/bin, you'll either need to use explicit full paths to them, or change the PATH variable to include the directories they're in.

Related

Execute bash commands in .txt file via cronjob

Set-up
I have 3 .txt files containing commands to be executed each day.
The berlin_run.txt file executes a.o. the 2 other .txt files. The file is the following,
#!/bin/bash
cd /path/to/folder/containing/berlin_run.txt
PATH=$PATH:/usr/local/bin
export PATH
./spider_apartments_run.txt
./spider_rooms_run.txt
python berlin_apartments_ads.py;python berlin_rooms.py
When I cd to /path/to/folder/containing/berlin_run.txt in my MacOS Terminal, and execute the ./berlin_run.txt command, everything works fine.
It is my understanding that ./ opens the berlin_run.txt, and that #!/bin/bash ensures that the subsequent lines in the berlin_run.txt are automatically executed upon opening.
Problem
I want to automate the execution of berlin_run.txt.
I have written the following cronjob,
10 13 * * * /path/to/folder/containing/berlin_run.txt
It is my understanding that this cronjob should open the berlin_run.txt each day at 13:10. Assuming that is correct, #!/bin/bash should execute all the subsequent lines. But nothing seems to happen.
Where and what am I doing wrong here?

run shell script in one line from applescript and provide input

I'm trying to automate the installation of a program that needs to be run from the Terminal on MacOS. This seems simple enough but the installation script expects needs to run as root (or sudo) and find files from the root directory that it's running from and then looks for the user to provide a password as terminal input before it completes. I don't mind providing the password in a readable format as we will only be using it internally and the password will be changed soon after we're finished with this project.
I have the following so far and it's not working because it doesn't seem to accept my input from the response file. I'm using the sleep to wait the few moments that it takes to execute that first step.
do shell script "cd ~/Desktop/; ./setup; /bin/sleep 5; ~/Desktop/MyApp/Tools/RunThis_WithVarialbles -s blah.com -someOtherVaiable -u uName#blah.com < ~/Desktop/sccm/Tools/response.txt" password "passwdIDCisVisible`enter code here`" with administrator privileges
The error I get from Script Editor is:
error "/bin/sh: ./setup: No such file or directory
ERROR 2 when mkdir in CreateDirectory - No such file or directoryERROR 2 when mkdir in CreateDirectory - No such file or directory
Please enter your password.
(null)(null)(null)(null)(null)" number 8
I'll be the first to admit that I'm a total Noob! Thanks for your patience!
Ok, so I can see a couple of wrongs in your command already. And it still seems pretty unclear what you are trying to achieve. Yet, instead of asking a lot of questions and causing confusion, let's just stick to the first step.
The first part of your error message reads: error "/bin/sh: ./setup: No such file or directory. So this could indicate that:
~/Desktop/setup does not exist. (Check if it does).
~/Desktop/setup is not properly specified and you need to add a file extensions (e.g. like ~/Desktop/setup.sh)
You do not have the proper rights to execute the file. So you should execute something like chmod u+x to give execution rights to your current user. (And sudo chmod u+x for root).
See if you can get passed that first. Learning to code usually means taking baby steps at first.
Good luck.

monitor for file then copy to another directory

I'm relatively new to unix scripting, so apologies for the newbie question.
I need to create a script which will permanently run in the background, and monitor for a file to arrive in an FTP landing directory, then copy it to a different directory, and lastly remove the file from the original directory.
The script is running on a Ubuntu box.
The landing directory is /home/vpntest
The file needs to be copied as /etc/ppp/chap-secrets
So far, I've got this
#/home/vpntest/FTP_file_copy.sh
if [ -f vvpn_azure.txt ]; then
cp vvpn_azure.txt /etc/ppp/chap-secrets
rm vvpn_azure.txt
fi
I can run this as root, and it works, but only as a one off (I need it to run permanently in the background, and trigger each time a new file is received in the landing zone.)
If I don't run as root, I get issues with permissions (even if I run it from within the directory /home/vpntest.)
Any help would be much appreciated.
Updated: crontab correction and extra info
One way to have a check and move process in background with root permissions, is the "polling" approach done via root user's crontab, with your script.
Steps:
Revise your /home/vpntest/FTP_file_copy.sh:
#!/bin/bash
new_file=/home/vpntest/vvpn_azure.txt
if [ -f "$new_file" ]; then
mv $new_file /etc/ppp/chap-secrets
fi
Log out. Log in as root user.
Add a cron task to run the script:
crontab -e
If this is a new machine, and your first time running crontab, you may get a prompt first to choose an editor for crontab, just choose and continue into the editor.
The format is m h dom mon dow command, so if checking every 5 minutes is sufficiently frequent, do:
*/5 * * * * /home/vpntest/FTP_file_copy.sh
Save and close to apply.
It will now automatically run the script every 5 minutes in the background, helping you to move the file if found.
Explanation
Root user, because you mentioned it only worked for you as root.
So we set this in the root user's crontab to have sufficient permissions.
man 5 crontab informs us:
Steps are also permitted after an asterisk, so if you want to say
'every two hours', just use '*/2'.
Thus we write */5 in the first column, which is the minutes column,
to set for "every 5 minutes".
FTP_file_copy.sh:
uses absolute paths, can run from anywhere
re-arranged so one variable new_file can be re-used
good practice to enclose any values being checked within your [ ] test
uses mv to write over the destination while removing itself from the source directory

Create a bash script that runs and updates a log whenever a file is deleted

I am new to bash scripting and I have to create a script that will run on all computers within my group at work (so it's not just checking one computer). We have a spreadsheet that keeps certain file information, and I am working to automate the updating of that spreadsheet. I already have an existing python script that gathers the information needed and writes to the spreadsheet.
What I need is a bash script (cron job, maybe?) that is activated anytime a user deletes a file that matches a certain extension within the specified file path. The script should hold on to the file name before it is completely deleted. I don't need any other information besides the name.
Does anyone have any suggestions for where I should begin with this? I've searched a bit but not found anything useful yet.
It would be something like:
for folders and files in path:
if file ends in .txt and is being deleted:
save file name
To save the name of every file .txt deleted in some directory path or any of its subdirectories, run:
inotifywait -m -e delete --format "%w%f" -r "path" 2>stderr.log | grep '\.txt$' >>logfile
Explanation:
-m tells inotifywait to keep running. The default is to exit after the first event
-e delete tells inotifywait to only report on file delete events.
--format "%w%f" tells inotifywait to print only the name of the deleted file
path is the target directory to watch.
-r tells inotifywait to monitor subdirectories of path recursively.
2>stderr.log tells the shell to save stderr output to a file named stderr.log. As long as things are working properly, you may ignore this file.
>>logfile tells the shell to redirect all output to the file logfile. If you leave this part off, output will be directed to stdout and you can watch in real time as files are deleted.
grep '\.txt$' limits the output to files with .txt extensions.
Mac OSX
Similar programs are available for OSX. See "Is there a command like “watch” or “inotifywait” on the Mac?".

Open terminal and it reads a line to me?

I know the settings were changed so that everytime I open my terminal it reads my name to me via the say command. Where is the file that it was saved in (so I can delete that line)?
It sounds like the say command has been added to one of your shell’s startup files. Try this command to search for the say command in all of them:
grep say ~/.bashrc ~/.bash_profile ~/.bash_login /etc/bashrc /etc/profile
If that doesn’t turn anything up, you can run sudo filebyproc.d to start a trace of all files being access on the filesystem, open a new terminal, and look for which files are read by the bash command. The filebyproc.d(1m) manpage has more info if you’re forced to go down this route.

Resources