how to query posts and auto assign category if post title has keyword

The first code will only do it for posts matching your query – i.e posts published that day. Something like

$query = new WP_Query( 'post_type=post' );

should retrieve all the posts, and then you could loop through them and perform the necessary operation.

A word of caution. Whenever a post is published, your function is run – so with every new published post, all your posts are checked and (if necessary) added to the category. To save time / unnecessarily querying the database, you might want to use the first function only once (maybe trigger it with a button click) and then use the second function for all new posts, which only check the post being published.

Answer to updated question
If you want to schedule to repeat the function then use:

<?php wp_schedule_event($timestamp, $recurrence, $hook, $args); ?>

$timestamp is a UNIX timestamp for when the scheduled first occurs. $recurrence is a string to describe how regular it should happen (e.g. ‘daily’, ‘hourly’ etc), $hook is your function name as a string, i.e. ‘update_post_terms’. Lastly $args is an array of (optional) arguments for your function.

As it stands though, your function takes $post_id as an argument and only updates that post. You would perhaps need another function (which is called periodically) to loop through all your posts and call the above function, if you wanted to update all posts (daily, hourly etc).

See the Codex page for more detail.

Hope this helps!