I'm trying to execute a bash script on Linux startup, but it doesn't work. I have tried all of these commands in the crontab:

@reboot bash /home/user/mysqlamazon.sh
@reboot sh /home/user/mysqlamazon.sh
@reboot /home/user/mysqlamazon.sh
@reboot sleep 60 && /home/user/mysqlamazon.sh

I have another command on crontab which works perfectly:

@reboot pwsh file.ps1

And I have also tried this command:

@reboot pwsh file.ps1 && sh /home/user/mysqlamazon.sh

None of these work! Any help would be appreciated!

Here is the content on bash script:

./transfermysql.sh > file.txt
bcp tablename in file.txt -S ***********.com,**** -U **** -P *********** -d ********* -c
:> file.txt
sleep 60

You don't tell us how this fails, but I am guessing you don't see it executed.

First of all, your script will never work since while($true) isn't valid shell syntax. I assume you want something like this:

while(($true)); do ... ; done

The more common idiom for that is:

while : ; do ... ; done

Or (true is a command):

while true; do ... ; done

This is most likely because you are using relative paths in your script:

./transfermysql.sh > file.txt

Replace that with the full path:

/path/to/transfermysql.sh > /path/to/file.txt

Next, I also suspect that bcp is not in cron's PATH, so use the full path to that as well:

/path/to/bcp tablename in file.txt -S ***********.com,**** -U **** -P *********** -d ********* -c

Finally, I don't know why you would want :> file.txt since your first command overwrites its contents anyway, but if you do need it for some reason, you need to use the full path there too: > /path/to/file.txt.

  • Also, the while($true). It actually works, but it's bogus as it relies on the variable true having an empty value, or being o simple command that returns success. I.e., running this with true=false would not work. Better with while true; do ...; done.
    – Kusalananda
    May 5 at 8:51
  • @Kusalananda yes, works if $true is the name of a command. Or if it is a non-0 number and you use while (($true)).
    – terdon
    May 5 at 8:57
  • @SoftProgrammer because the cron script is likely not running from your home directory. In any case, as I said in the answer, you don't tell us how it "does not work" so all I can do is guess.
    – terdon
    May 5 at 9:11
  • @terdon thanks a lot. The problem was on specifying the path of bcp. May 5 at 9:44
  • Always paste your script into https://shellcheck.net, a syntax checker, or install shellcheck locally. Make using shellcheck part of your development process.
    – waltinator
    May 5 at 13:59

Jobs run through cron, or at, or batch, aren't run in the same runtime environment that you have on your desktop. None of your PATH changes, or other environment variable settings are automatically propagated to your cron job. For example, there's no $DISPLAY, so GUI programs need special treatment (read man xhost).

One can set environment variables for all one's cron jobs in the crontab file Read man 5 crontab.

Look at the results of echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias in each of your environments.

Since the command part of the crontab line is, by default, interpreted by /bin/sh, which has a simpler syntax than /bin/bash, I recommend having command be a call to a bash script (executable, mounted, starts with #!/bin/bash) which sets up the environment, then calls the desired program.

  • Did you mean to post this as an answer to this question? What environment? What variables? Please don't post identical answers. Either the questions are the same (so vote to close as duplicates) or adapt the answer to each question.
    – terdon
    May 5 at 14:02
  • While this answer is generically true, it doesn't answer the question in a useful (specific) way. This answer would be useful to troubleshoot the situation, but clarifying the situation is better done as comments to the Question. Then the question can be updated with the details, and once the actual problem has been found, a specific Answer can be written up.
    – Jeff Schaller
    May 7 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.