How do I make a script or program to run certain commands? [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to get into writing scripts that execute in the terminal. I wanted to know what the best way to do this was. I want to start by making a simple script that will run four or five commands that will update a certain program on my computer and have that run every day at a certain time. I have a programming background, but I am unfamiliar with this kind of scripting. I would appreciate any advice or input such as what language to use.

First of all, you need to open a Terminal (such as Terminal, Terminator, etc) and then you run this:
touch myScript.sh
chmod 755 myScript.sh
The first command creates an empty file and then you give 755 permissions to it. It means that it will be readable and executable by any user in your machine. If you need more details about those permissions, you can refer to the documentation here. But, believe me, those permissions will work for the moment.
Now you can insert instructions into the file using several methods: You can open it with a text editor such as vi, etc; Also, you can echo those commands this way:
echo "ls /tmp" >> myScript.sh
echo "echo 'hello'" >> myScript.sh
echo "pwd" >> myScript.sh
If you open that file, you can find that it is simply a list of commands one at each line. Then, when you run the script, each command will be executed in order from top to bottom.
You can run the script using the following sintax:
./myScript.sh
Voilá!

crontab -e
Then add following line beside the above command:
30 10 * * * script.sh
It will run everyday at 10:30 AM

Related

Crontab on linux [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I can't understand, I have looked at several forums to help me, etc... But it still doesn't work!
I would like to run a script thanks to Cron!
To try, I'm currently trying to execute a script that sends "test" in commands (with echo test). No problem, this script works perfectly by hand if I call the file.
But I tried to run my file with Crontab -e every minute and I've been waiting for several minutes already, but no results.
And I can't really understand why!
Already, I was told to put #! /bin/bash at the beginning of my code in my script, but when I put it in I have an error and the code doesn't execute by hand. Whereas if I don't put anything in, the code runs smoothly.
So I don't know if that's where the mistake came from, maybe.....
The final goal would be to make a script that runs every day, say to clear the cache with sync; echo 3 > /proc/sys/vm/drop_caches.
What should look like in the Crontab : 00 20 * * * PATH if I'm not mistaken.
Do you have a solution to help me?
EDIT: -bash: /root/Discord/script/cache.sh: /bin/bash^M: bad interpreter: No such file or directory it's the error I got when I runned /root/Discord/script/cache.sh to execut my script. And that command works when I don't have #! /bin/bash. But that works when I used sh cache.sh in the directory, even with #! /bin/bash !
Crontabs don't print the output on your opened terminal. You need to either create a file and then append the output there to view or test if it works. You can refer this answer https://stackoverflow.com/a/28856563/7181668
But if you want to run a shell script file through cron then you need to make sure you have given executable permission to that file and then you can use the below command in crontab -e
* * * * * /bin/sh /home/myUser/scripts/test.sh

Execute the shell script that is in the current directory, not the one in $PATH [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have defined the path in .bash_profile file for /usr/share/totalscripts as:
PATH="${PATH}:/usr/share/totalscripts"
export PATH
Where all my shell scripts programs are present - /usr/share/totalscripts
Specifically, I need to execute the scripts in the current directory, not the one available in my $PATH environment variable. That is, executing a specific script rather than any script that matches that is found in the PATH.
For example, it might make sense to not call script1.sh (which will execute against any script1.sh found in the PATH), but instead call ./script1.sh (which will only execute the script that is found in the local directory).
This because I have the same script under /usr/share/totalscripts/script1.sh and /home/script1.sh (same names)
If you are working in a script, then you can define your environment variables at the start of it, like:
MY_VAR="/my/path/to/scripts"
And then call your script from within that script using:
${MY_VAR}/my_script.sh
Kind of the same as running the script using its full path.
I hope this helps!
Edit 1: - If you want to run a specific script as you mentioned in the comments, under a specific directory, you can try changing directories to the one that contains the file, and then running the script like ./script1.sh.
cd /path/containing/your/scripts/
./script1.sh
Edit 2: - Maybe you can create a wrapper for your script1.sh, placing the following if-block in there to check if the script is under the current directory or not.
if [ -e script1.sh ]; then
./script1.sh # runs the script under your current directory
else
script1.sh # runs the script searching for it in $PATH first
fi

Cron says "errors in crontab file, cannot install" [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm attempting to execute the following series of commands to create backups of MySQL databases.
When I attempt to add the command to my crontab using crontab -e I get the error "errors in crontab file, cannot install" and asks me if I want to retry.
mkdir /home/mysql-backup/`date '+%m-%d-%Y'`; mysql -s -r -e 'show databases' | while read db; do mysqldump $db -r /home/mysql-backup/`date '+%m-%d-%Y'`/${db}.sql; done; rm -r -f `date --date="1 week ago" +%m-%d-%Y`; du -k |sort -n > output; mail -s "MySQL Backups" "steven#brightbear.net" < output
Is there anything I should be changing in this file? Or should I look into creating a script file and calling that from cron.
Thanks in advance for any assistance you can provide.
If you gave that script to crontab -e of course it will disagree. A line in a crontab file should start with 5 fields indicating when you want the script to run, as can be read in crontab's manpage.
On the other hand, most Linux distros nowadays have preset facilities for things that should be executed hourly (/etc/cron.hourly), daily (/etc/cron.daily), etc. It's a whole lot easier to just put your script in a file in the appropriate directory and it will get executed in the selected time raster. An added advantage is that in these files you won't be forced to cram everything into one line.
Yes; as a matter of style, if nothing else, I encourage to put the SQL commands into a shell script, and then run the shell script from cron.  (And, as Anew points out, the command sequence is easier to maintain/debug if it’s broken out into multiple lines, with comments.)  But –– is that all of what you’re feeding into crontab?  Look at man crontab and add the fields that specify when you want the command to run.
From the crontab(5) man page, it looks like percent signs (%) have a special meaning, so that is probably where you're running into trouble.
Yes, you should put your commands into a separate shell script and just call that from the crontab line. This will also make it much easier to read the crontab file, and you can format your script nicely so that it's easier to maintain. And you can test it separately from the crontab that way.

Crontab that executes shell script every minute [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have made a shell script on my red hat server that needs to be run every minute. My script is located in /media. I have edited the crontab like so:
* * * * * /media/statusshellscript.sh
My script is definitely in the location above and I know that 5 stars means run every minute.
oh.. and my script definintely works! because when I do a ./statusshellscript it works fine. Here is my script anyway, it basically just runs a php script I made which made life easier.
#!/bin/bash
# Script to execute the PHP Script
cd ~
cd /media/PHPServerTest
php -f index.php
Crontab is doing absolutely nothing at the moment. Not sure what to try next?
Also.. permissions shouldn't be a problem as i've done chmod 777 statusshellscript.
if its not running though cronjob but by command its working fine then there can be two reasons
1) you never made your file executable , that you can resolve my using the command
sudo chmod +x filename
2) your path is not correct , for finding absolute path you can use command
realpath(filename)
if realpath is not already installed it will mention you a command how to install it
by checking these points it should work fine.
The PATH for a crontab is not the same as in a shell.
Make sure that you define a PATH in your crontab that includes everything that is needed by the script.
Also, make sure that the script starts with a valid #! marker that points at the desired shell.
Or, use the full path for all commands in the script.
As others have said, my bet would be a misconfigured PATH. Try putting this into your path:
"* * * * * /media/statusshellscript.sh"
Go check that output file to see the PATH when the script is run. And rather than defining your PATH in the crontab, just define it in your script.

saving cron job in ubuntu [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I am new to ubuntu and cron job. I have entered the following into my command line:
crontab -e
and I get the following output: "no crontab for teddy - using an empty one 888"
Then I enter when I want it to execute (I believe this is right?... I want it to run once a day, everyday at 8pm):
00 18 * * * /*****/*****/****/test.php
Here is my problem, I dont know how to exit back to the command line. Everything I type gives me weird looking letters and enter (return) doesn't do anything. I have read that this will do the job
ESC : w q
but its not working for me. I tried typing that in, I tried pressing them at the same time, I tried pressing one at a time. Nothing, still stuck. When I press ESC, it comes out as ^[.
This is probably very easy question and I apologize if it is stupid but I have been stuck for sometime. Any help would much appreciated.
Thank you
P.S. I read somewhere that if this is your first job that you need to to do an end line at the end of the cronjob... is this a simple enter key press or actually typing \n?
Teddy13, let's get some clarifications here.
Ubuntu is the distribution of Linux you are using. None of the commands you're typing are exclusive to Ubuntu.
You're asking questions about two separate issues. One is "How do I write a crontab". The other is "How do I use vi, the default editor of the crontab command".
First, man crontab to review the format for entries in the file. Note that cron runs things that are executable from the shell. You can only run your "test.php" script if it is structured like a shell script, with the first line containing "shell magic" (i.e. something like #!/usr/local/bin/php).
Second, while vi is a powerful and well-loved text editor, it's not the easiest to use. I fully support any efforts you may make to learn how to use it, but until you're comfortable with it, you might want to consider switching to "pico" or "ee" or "joe", which are all much easier to learn, though they can do much less. You can install joe, for example, with the command: apt-get install joe run as root. Then to use joe to edit your crontab, add export VISUAL=/usr/bin/joe to the .bashrc file in your home directory.
There's a lot of background information you may want to get. Read lots. It's worth it.
UPDATE (per comment):
Here's the basic stuff you need to edit your crontab.
crontab -e ... as you now know, this edits your crontab file using $EDITOR or $VISUAL, which defaults to vi.
Inside vi, you are always in one of three modes. MOVEMENT mode lets you move around the file using arrows or H, J, K and L. You can delete lines with "dd" or characters with "x". EDIT mode lets you add or change text. From Movement mode, use "i" or "a" or "o" to enter Edit mode in different ways. Read docs for details. Third, COMMAND mode can be reached by hitting ":" from Movement mode. From here, you can issue a variety of commands to save, search, bulk edit, etc.
From Movement mode, you can save your file and exit using "ZZ". From edit mode, you can do this with the command "wq" (hence the ":wq" mentioned elsewhere).
Alternately, you can set a new crontab by piping information into the crontab command. Note that this will erase any existing crontab you may have. Run this in your shell updating the URL of your script as required:
echo "0 20 * * * wget http://example.com/path/to/file.php" | crontab -
crontab -l will show you what your current crontab contains.
Hope this helps.
UPDATE #2 (per comments below):
$ tmpfile=/tmp/foo.$$
$ crontab -l > $tmpfile
$ echo "30 6 * * * Mail -s wakeup pager#example.net <<< 'Time to wake up.'" >> $tmpfile
$ crontab - < $tmpfile && rm $tmpfile
You can try Ctrl+X. This will ask you for changes to save or not before exit. In that just give Shift+Y so you will get out of there. Try it.
You need to press ZZ to exit (upper case 'z' twice) . Here is an article on setting up cron tabs - http://www.sophos.com/support/knowledgebase/article/12176.html

Resources