I used crontab to run a bash script once a day (at midnight). This worked successfully, but I didn't really think it through enough to realise that the script would not run if my computer happened to be off at midnight (or even asleep).
So I think that I have to keep the crontab entry (in case my computer is on) but I also want to make the bash script execute on startup (in case my computer was off) so that I have covered all my bases.
I saw online that to have my script execute on startup I should edit my /etc/rc.local file. My file now contains:
#! /bin/bash
bash /home/me/code.sh
exit 0
and my permissions are -rwxr-xr-x.
However, the script does not execute on startup. Is there something I'm doing wrong?
I guess you created the /etc/rc.local yourself right? AFAIK there isn't any in Ubuntu 18.04 anymore (as it is depricated). That's probably why it's not executed.
#reboot in crontab might be a possible solution for your problem.
Anacron is the answer for your problem. This is cron like system created exactly for tasks on machines not always on.
https://linux.die.net/man/5/anacrontab
Related
Is there a way to automate a task (zsh shell script) using some type of scheduler. If so, how?
For example, if I am running a zsh shell script within my system named zshprogram1.zsh within the directory /Users/myname/zshtest and I want to run it at 1:35 PM HST daily, how would I go about doing that? Also, I'm assuming it would only work if my laptop is on. Will it work if it's on but in sleep mode?
Check out crontab or launchctl.
For example, with crontab you would run crontab -e from Terminal and paste the following line into the editor that comes up
35 13 * * * /Users/myname/zshtest/zshprogram1.zsh
to have zshprogram1.zsh run at 1:35 PM every day. Launchctl is a bit more complicated to configure, but Apple recommends it over crontab for the future, so it might be worthwhile to check out.
I was trying to get a crontab working on my Raspberry PI and I think I messed up my environment variables. I can execute a file from the GUI by right-clicking and choosing execute. However I cannot get the same file to run from command line. I can use ls to see the file (ChromeTab.sh), but when I type ChromeTab.sh, I get "bash: ChromeTab.sh: command not found".
I think I messed up my environment variables when I put this in the crontab.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
I followed the examples in Script doesn't run via crontab but works fine standalone.
Any idea what I'm doing wrong?
UPDATE:
OK,
Let me clarify what efforts I took on my part BEFORE posting my question on stackoverflow before getting anymore downvotes.
First of all thanks S. Adam Nissley for your suggestions.
In answer to your steps listed above.
Running this from home path, or fully qualified path does Not work as stated.
Error: bash: ChromeTab.sh: command not found
./ChromeTab.sh
I have also ensured read/write and execute permissions on the file with
chmod +x ./ChromeTab.sh
Also, my bash script starts off with the following shebang
#!/bin/sh
So, what i'm trying to say is, regardless of using crontab or not the issue at hand is that I can not even execute the script from command line. This started happening after I was messing around with the environment variables in the crontab. I'm looking for a way to revert to the situation where I can at least run/execute bash commands from the terminal.
The only way I can effectively execute this script is (right-click execute) through the GUI.
Assuming you are in the same directory as your script, you should just be able to enter
./ChromeTab.sh
If it does not execute, make sure it is executable with the command
chmod +x ./ChromeTab.sh
Or
chmod 755 ./ChromeTab.sh
And if it still won't execute, make sure it has an appropriate hashbang on the very first line of the script like #!/bin/sh or #!/bin/bash
When you add it to your crontab, make sure it has the full path like
/home/pi/bin/ChromeTab.sh <br/>
EDIT: Default PATH and SHELL for Raspbian
You can check your PATH and SHELL environmental variables from the command line as follows:
echo $SHELL
echo $PATH
The default PATH for Rasbian is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
And the default SHELL is:
/bin/bash
So if you need to set those it is as simple as:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
SHELL=/bin/bash
If you are having other issues with your environment, you may want to disable some of your local settings to see if the problem is in your profile. You can list all files with ls -a, which includes hidden files. Raspbian typically has a .bashrc and a .profile in each user's home directory. To disable them simple rename them:
mv .bashrc .bashrc_disabled
mv .profile .profile_disabled
If that solves the problem, you can inspect the files and make the necessary corrections before renaming them back to their original names.
I just realized that i cannot execute .sh scripts anymore on my debian.
It used to work fine. All .sh files are +x chmodded and used to work fine.
Suddenly, once i execute: ./test.sh system hangs.
I am able to stop this via ctrl+c, but script never executes.
Steps did so far was to restart my VPS.
Any ideas?
I am pretty sure shell scripts are still working on your machine, because if they weren't, you would not be able to complete any boot cycle.
If I suppose you are facing problems with your homemade scripts, then you should probably check your shebang line (#!/bin/sh for example) to see if anything unusual was used there.
New scripts are using a dispatcher to interpreters:
`#!/usr/bin/env bash` #lends you some flexibility on different systems
`#!/usr/bin/bash` #gives you explicit control on a given system of what executable is called
The difference between “#! /usr/bin/env bash” and “#! /usr/bin/bash”?
I am trying to run a server. the command x_server.sh do not work for my ubuntu 14.04 but it runs when I give the command bash x_server.sh.
It has adviced that the products should not run as a daemon thread.
What I want to know is what exactly this bash command do, is it run as a daemon thread when i do so and what are the alternative ways there for me to use to make that command x_server.sh work.
On Ubuntu the default shell is Dash, not Bash. Presumably your x_server.sh script starts like this:
#!/bin/sh
You should change it to this:
#!/usr/bin/env bash
That will make it auto-select the best "bash" on the system rather than the default shell which is dash which has different (mostly fewer) features than bash.
And of course you need to do the usual chmod +x x_server.sh to make it executable in the first place, and run it as ./x_server.sh unless it's in your $PATH.
I have a script which runs perfectly fine while it is run in the shell.
But runs incompletely or partially while the same script is scheduled in cron job.
It gets terminated abruptly
I have scheduled the cron job to run every 15mins.
Here is the code for the cronjob.
0,15,30,45 * * * * /home/convteam/Conversion/update_revenue/RunUpdateRevenue.ksh > /home/convteam/Conversion/update_revenue/RunUpdateRevenue.log
I'm scheduling the job using a user called as 'convteam' and not as a root.
Can you help me what I'm missing here ?
This is a recurring issue. The most common reasons for cron jobs failing are:
Assumptions about relative file paths - use full absolute filenames throughout
Assumptions about the environment. You should "source" start-up files that you rely on, typically (for ksh):
. /etc/profile
. ~/.profile
. "$ENV"
$ENV is optional, but when used usually has the value ~/.kshrc.
crontab invokes shell in an non-login, non-interactive mode. Bash shell in this mode does not load the usual startup files such as /etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile, ~/.bashrc, which may loose some important environment varible definations like PATH.
You may solve your problem by sourcing particular startup files (/etc/profile, ~/.profile in ksh if they exist and are readable) before run ksh script when runing in crontab. you can "man ksh" to see the details of ksh startup process.