I've created a script and copied it to another server and try to execute it at the same time but I'm getting a permission error.
I'm logged on as root and I gave the file 777 permission when I created it, but I can't run it remotely via ssh command because of permission issue.
Ack! It's rare that a file needs 777 permissions, especially one that is owned by root. But if you want to keep the permissions, change the scp line to:
Where in the script are you declaring ${script_name} ?
You didn't post the entire script, but by the message you are getting, it seems that ${script_name} is null and what the ssh command is trying to execute is just ./
BTW, you don't need to cd then execute. Just do
ssh hostname /usr/local/bin/script
Also, your scp command pretends to copy ${script_name} to /usr/local/bin, but your ssh command goes to /usr/local/bin/userid.