I am trying to drop an entire table within a database. My intention is simple, and I wish for this to happen occasionally through a scheduled crontab command.
For now, I have this example cronjob attempt (which does not work if you change the schedule to something closer for testing):
0 0 1 1 * mysql -u root -p <passwordWouldBeHere> -D Survival -h localhost -e 'DROP TABLE stats;'
Does this seem correct?
However, this does not work. My only suspicion is that it's possibly due to the content of the password, which contains a & character within it, I suspect part of the password may be detected as an additional separate argument within the command, even though the whole password is concatenated without spaces.
I had to install a mail server on my host just to get the return arguments each time this and any other command scheduled on crontab is executed (singular, but I guess that's how crontab works).
Thanks to this though, I now get the return error message from the faulty command I've shown as example:
/bin/sh: 1: <part of the password>: not found
The message is basically telling me that part of the password was not found, as though that chunk where an extra argument. I haven't found any manuals explaining the whole mysql command syntax, so I do not know if '&' could actually represent the start of a new parameter, thus causing this error. If this where the case, could I maybe insert some special exclusion character within the password just to avoid it from being detected as a separate parameter?
I have not tried executing the command manually on the regular shell. I can do it, but I'd prefer to get it right directly in crontab. I'm guessing I'd get a similar return message as error.
I am using 10.3.16-MariaDB And Ubuntu 18.04
I did my best to include all relevant information on this question, so if you find something is missing, please point to it politely and I will correct/add content. Thanks in advance for any help.
21 Answer
Solution: Changed the syntax to:
30 22 7 8 * mysql --host localhost --user root --password="<passwordWouldBeHere>" -D Factions -e "TRUNCATE TABLE stats;" ;
Also decided to truncate instead of drop.