How to drop a table within a database in a single line, scheduled from crontab?

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.

2

1 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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like